startLeScan():在IntentService上为null

时间:2016-06-27 12:36:12

标签: android bluetooth-lowenergy

我为Beacon检测实现了IntentService,在启动BLE设备检测时遇到了非系统性问题。

这是我服务的示例代码:

public class BeaconService extends IntentService{
...
...
    @Override
protected void onHandleIntent(Intent intent) {
...do initialization stuffs....

    startBLE();     

}

    public void startBLE(){

    mHandler = new Handler();
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Log.d("BeaconService", "Erreur Bluetooth pas de feature Bluetooth sur le mobile...");
    }
        // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    mBluetoothAdapter = bluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.enable();
        Log.d("JRE","enabling BLE");
    }

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Log.d("BeaconService", "Erreur d'adapteur Bluetooth...");
        return;
    }

    mHandler.post(scanRunnable);

}



private Runnable scanRunnable = new Runnable()
{
    @Override
    public void run() {

        Log.d("JRE","Round Scan for Beacon started");

        if (isScanning)
        {
            if (mBluetoothAdapter != null)
            {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);

            }
        }
        else
        {
            if (mBluetoothAdapter != null)
            {
                mBluetoothAdapter.startLeScan(mLeScanCallback);
            }
        }

        isScanning = !isScanning;

        mHandler.postDelayed(this, scan_interval_ms);
    }
};

 private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

                Log.d("BeaconService","onLeScan callback");
        .....do my stuff on detection....

我遇到了一种奇怪的行为,因为有时我会收到错误:

startLeScan(): null
06-27 14:25:14.115: I/Timeline(29500): Timeline: Activity_launch_request     id:com.jre.ordolink time:43106423
06-27 14:25:14.185: E/ViewRootImpl(29500): sendUserActionEvent() mView == null
06-27 14:25:14.325: D/BluetoothLeScanner(29500): onClientRegistered() - status=0 clientIf=6
06-27 14:25:14.625: D/PhoneWindow(29500): *FMB* installDecor mIsFloating : false
06-27 14:25:14.625: D/PhoneWindow(29500): *FMB* installDecor flags : -2139029248
06-27 14:25:14.965: I/art(29500): Background sticky concurrent mark sweep GC freed 12983(984KB) AllocSpace objects, 7(160KB) LOS objects, 0% free, 40MB/40MB, paused 1.210ms total 314.016ms
06-27 14:25:15.065: W/MessageQueue(29500): Handler (android.os.Handler) {1e3fad36} sending message to a Handler on a dead thread
06-27 14:25:15.065: W/MessageQueue(29500): java.lang.IllegalStateException: Handler (android.os.Handler) {1e3fad36} sending message to a Handler on a dead thread
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
06-27 14:25:15.065: W/MessageQueue(29500):  at     android.os.Handler.enqueueMessage(Handler.java:631)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Handler.sendMessageAtTime(Handler.java:600)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Handler.sendMessageDelayed(Handler.java:570)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Handler.postDelayed(Handler.java:398)
06-27 14:25:15.065: W/MessageQueue(29500):  at com.jre.ordolink.BeaconService$2.run(BeaconService.java:325)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Handler.handleCallback(Handler.java:739)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Handler.dispatchMessage(Handler.java:95)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.Looper.loop(Looper.java:145)
06-27 14:25:15.065: W/MessageQueue(29500):  at android.os.HandlerThread.run(HandlerThread.java:61)

2 个答案:

答案 0 :(得分:1)

我最后使用另一种方法通过以下方式使我的项目工作:

private BluetoothLeScanner mBluetoothLeScanner;  
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

mBluetoothLeScanner.startScan(mScanCallback);

//Scan call back function
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
     super.onScanResult(callbackType, result);
     mLeDeviceListAdapter.addDevice(result.getDevice());
     mLeDeviceListAdapter.notifyDataSetChanged();
}
};
希望它能帮助别人!

答案 1 :(得分:0)

我建议不要使用Handler扫描蓝牙设备。这是一个后台异步操作,默认处理程序线程是主要的。 使用你自己的线程。