0
我在定期进行蓝牙扫描时遇到问题。如果选定的设备在扫描范围之前和范围内成功扫描后超出扫描范围,将发生什么情况?播放闹钟并进行振动。android蓝牙异常接收DISCOVERY_FINISHED
但对于第一次后,由于某种原因,它超出范围,并通知我成功,下面的扫描失败,异常:
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 [email protected]
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)
我的代码和Reciever如下:
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();
}
}
};
你知道这是什么样的是,我以前改变了年龄..我忘了,我没有...感谢您的抬头。 。 奇迹般有效 –