安卓要如何在背景偵測呢?!

demo裡的要如何修改才能讓搜尋blueduino信號的動作在背景執行??

你可以开启个service 在servive中执行扫描

請問一下如果我完全的關閉程式,他會有推播的服務嗎?!

service在就可以通知 service不在就不可以

我寫了個service,調用你的的SDK connectToService() ,但我將APP關闢時,,而我寫在service的其他功能還在運作,卻唯獨不執行掃描的動作,請問是要調用甚麼程式碼嗎?!

@SuppressLint("NewApi")
public class IBeaconService extends Service {
int m_nTime = 0;
private BluetoothAdapter Adapter;
private BeaconAdapter adapter;
private BeaconManager beaconManager;
private Handler mHandlerTime = new Handler();
private static final Region ALL_BEACONS_REGION = new Region("customRegionName", null,
		null, null);
private ArrayList<Beacon> myBeacons;
@Override
public void onCreate() {
	super.onCreate();
	mHandlerTime.postDelayed(timerRun, 1000);
	init();
	// Kick off the process of building a GoogleApiClient and requesting the LocationServices
	// API.
}

@Override
public IBinder onBind(Intent intent) {
	return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	// Within {@code onPause()}, we pause location updates, but leave the
	// connection to GoogleApiClient intact.  Here, we resume receiving
	// location updates if the user has requested them.
	Adapter = BluetoothAdapter.getDefaultAdapter();
	Log.e("Bluetooth",Adapter.isEnabled()+"");
	if(Adapter.isEnabled()!=true){//如果藍芽未開啟
		/*Intent enableBtIntent = new Intent(
				BluetoothAdapter.ACTION_REQUEST_ENABLE);
		startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);*/
		Adapter.enable();
		connectToService();
	}else {
		connectToService();
	}
	Log.d("LOC", "Service init...");

	return Service.START_REDELIVER_INTENT;
}

public void connectToService() {
	//adapter.replaceWith(Collections.<Beacon> emptyList());
	beaconManager.connect(new BeaconManager.ServiceReadyCallback() {

		@Override
		public void onServiceReady() {
			try {
				beaconManager.startRanging(ALL_BEACONS_REGION);
				beaconManager.startMonitoring(ALL_BEACONS_REGION);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	});
}

private void init() {
	myBeacons = new ArrayList<Beacon>();
	adapter = new BeaconAdapter(this);
	//Log.e("JEFF",adapter.getCount()+"");
	AprilL.enableDebugLogging(true);


	// 此处上下文需要是Activity或者Serivce
	// 若在FragmentActivit或者其他Activity子类中使用 上下文请使用getApplicationContext
	beaconManager = new BeaconManager(getApplicationContext());
	//beaconManager.setMonitoringExpirationMill(0L);
	//beaconManager.setRangingExpirationMill(0L);
	//beaconManager.setForegroundScanPeriod(2000, 0);
	//beaconManager.setBackgroundScanPeriod(1000,0);
	beaconManager.setRangingListener(new BeaconManager.RangingListener() {

		@Override
		public void onBeaconsDiscovered(Region region,
										final List<Beacon> beacons) {
			if (beacons != null && beacons.size() > 0) {

				Log.e("NAME", beacons.get(0).getMacAddress());
			}
			Log.e("size", beacons.size()+"");
			if(beacons.size()!=0) {
				if (beacons.get(0).getDistance() < 1) {
					Toast.makeText(getApplicationContext(), beacons.get(0).getMacAddress(), Toast.LENGTH_SHORT).show();
					Log.e("MacAddress", beacons.get(0).getMacAddress());
					//sendNotification(beacons.get(0).getMacAddress());
				}
			}
			myBeacons.clear();
			myBeacons.addAll(beacons);
			adapter.replaceWith(myBeacons);
		}
	});

	beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {

		@Override
		public void onExitedRegion(Region region) {
			Toast.makeText(getApplicationContext(), "Notify in", 0).show();
		}

		@Override
		public void onEnteredRegion(Region region, List<Beacon> beacons) {
			Toast.makeText(getApplicationContext(), "Notify out", 0).show();
		}
	});

}


private final Runnable timerRun = new Runnable()
{
	public void run()
	{
		 // 經過的秒數 + 1
		String A = String.valueOf(m_nTime);
		Toast.makeText(getApplicationContext(),A,Toast.LENGTH_SHORT).show();
		mHandlerTime.postDelayed(this, 1000);
		++m_nTime;
		// 若要取消可以寫一個判斷在這決定是否啟動下一次即可
	}
};

@Override
public void onDestroy() {
	mHandlerTime.removeCallbacks(timerRun);
	//beaconManager.disconnect();
	super.onDestroy();

}

}

這是我的程式碼,可以幫我看看我哪裡搞錯了嗎?!

这个可能是手机省电机制 讲sdk中扫描关闭了 你要是希望可以使得扫描更加健壮 你可以看下我们的这个lib https://github.com/circlec/BleTool 这里也添加beaocn扫描并且相对之前的sdk 扫描更加健壮 不容易被关闭

倘若我只是將app縮到背景並沒有完全關閉,我要讓他寄宿搜尋一樣要寫service嗎?!

没有完全关闭 不用写service

可握在DEMO檔裡的Activity沒有看到onPause() & onResume(),將APP縮到背景時他就會跳出BluetoothAdapter: scan not started yet,這怎解呢?!

你测试机是用的是哪个手机 有没有设置类似省电模式等操作 其他测试机也是这种情况么

Samsung S7 edge 有省電模式可是我沒有開啟

https://github.com/circlec/BleTool 用这个在背景时也是无法扫描吗

我已經解決了背景掃描以及service,想請問一下 beacons.get(0).getName() 它顯示為 “abeacon_89FF”,這個是String ?!
為何我使用
if(beacons.get(0).getName() ==“abeacon_89FF”){
generateNotification(getApplicationContext(),beacons.get(0).getName(),activity,newsfeedid);
}
卻沒有功效呢?!

是String 你可以使用equals比较
==比较内容不仅需要相同 对象也要相同才返回true

太感謝您了,已解決問題

jeff大您好,想請問您ibeacon demo是如何改寫成service呢]