我在定期进行蓝牙扫描时遇到问题。如果所选设备在成功扫描之前和范围内后超出扫描范围会发生什么,播放警报并振动。
但是由于某种原因,在第一次超出范围并成功通知我之后,以下扫描失败并出现异常:
10-27 13:32:42.189: W/MediaPlayer(30256): info/warning (1, 902)
10-27 13:32:42.199: D/MediaPlayer(30256): start() in
10-27 13:32:42.219: D/MediaPlayer(30256): start() out
10-27 13:32:42.249: I/MediaPlayer(30256): Info (1,902)
10-27 13:32:55.091: D/AndroidRuntime(30256): Shutting down VM
10-27 13:32:55.091: W/dalvikvm(30256): threadid=1: thread exiting with uncaught exception (group=0x40ad9228)
10-27 13:32:55.101: D/Process(30256): killProcess, pid=30256
10-27 13:32:55.101: D/Process(30256): dalvik.system.VMStack.getThreadStackTrace(Native Method)
10-27 13:32:55.101: D/Process(30256): java.lang.Thread.getStackTrace(Thread.java:599)
10-27 13:32:55.101: E/AndroidRuntime(30256): FATAL EXCEPTION: main
10-27 13:32:55.101: E/AndroidRuntime(30256): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.adapter.action.DISCOVERY_FINISHED flg=0x10 } in com.moali.blue.service.MyService$1@40da2270
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:794)
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.os.Handler.handleCallback(Handler.java:605)
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.os.Looper.loop(Looper.java:154)
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.app.ActivityThread.main(ActivityThread.java:4945)
10-27 13:32:55.101: E/AndroidRuntime(30256): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 13:32:55.101: E/AndroidRuntime(30256): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 13:32:55.101: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-27 13:32:55.101: E/AndroidRuntime(30256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-27 13:32:55.101: E/AndroidRuntime(30256): at dalvik.system.NativeStart.main(Native Method)
10-27 13:32:55.101: E/AndroidRuntime(30256): Caused by: java.lang.NullPointerException
10-27 13:32:55.101: E/AndroidRuntime(30256): at com.moali.blue.service.MyService$1.onReceive(MyService.java:107)
10-27 13:32:55.101: E/AndroidRuntime(30256): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781)
10-27 13:32:55.101: E/AndroidRuntime(30256): ... 9 more
10-27 13:32:55.111: D/Process(30256): android.os.Process.killProcess(Process.java:791)
10-27 13:32:55.111: D/Process(30256): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
10-27 13:32:55.111: D/Process(30256): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
10-27 13:32:55.111: D/Process(30256): java.lang.ThreadGroup.unca ughtException(ThreadGroup.java:690)
10-27 13:32:55.111: D/Process(30256): dalvik.system.NativeStart.main(Native Method)
我的代码和接收器如下:
public void startVibrate()
{
//Set the pattern for vibration
long pattern[]={0,200,100,300,400};
//Start the vibration
//start vibration with repeated count, use -1 if you don't want to repeat the vibration
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
try {
mr = MediaPlayer.create(MyService.this, alert);
mr.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
mr.reset();
mr.release();
vibrator.cancel();
}
});
} catch (Exception e) {
}
vibrator.vibrate(pattern,0);
mr.start();
}
protected void discover() {
if(bluetoothAdapter.isDiscovering())
bluetoothAdapter.cancelDiscovery();
bluetoothAdapter.startDiscovery();
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
@SuppressLint("NewApi")
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String sDevice = device.getName();
if(!sDevice.equals(null) && sDevice.equalsIgnoreCase(selected)){
matchTo=device;
inRange= true;
if(bluetoothAdapter.isDiscovering())
bluetoothAdapter.cancelDiscovery();
}
}else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
if(!matchTo.equals(null)){
if(inRange){
inRange=false;
}
else{
matchTo=null;
startVibrate();
}
}
discover();
}
}
};