2016-06-27 11 views
0

私はBeacon検出用のIntentServiceを実装しましたが、BLE Devices検出を開始する際に非体系的な問題が発生しました。ここstartLeScan():IntentServiceのnull

は私のサービスのサンプルコードです:

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) 

答えて

0

私はようやく私のプロジェクトは、以下を使用して作業を取得する別の方法を使用:

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(); 
} 
}; 

は、それが誰かを助けることを願っています!

0

私はスキャンのためHandlerを使用しないことをお勧めBluetoothデバイス用。これは背景の非同期操作で、デフォルトのハンドラスレッドはmainです。 独自のスレッドを使用してください。

+0

このエラーが発生する可能性がありますか? – tiamat

+0

これはあなたのスタックトレースが示唆しているものです。 –

+0

私は試しましたが、もう例外はありませんが、まだstartLeScan():null – tiamat

関連する問題