0

Bluetooth スキャンを定期的に実行する際に問題が発生しています。範囲内で正常にスキャンされた後、選択したデバイスがスキャン範囲外になった場合、アラームを鳴らして振動させます。

しかし、何らかの理由で、最初に範囲外になり、正常に通知された後、次のスキャンは例外で失敗します:

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

    }

};
4

1 に答える 1

0

matchTo オブジェクトが有効かどうかを確認する必要がありますが、構文が正しくありません。オブジェクトが有効かどうか (null でないかどうか) を確認するために、オブジェクトのメソッド ( equalsなど)を呼び出すことはできません。

それ以外の:

if(!matchTo.equals(null)){

使用する:

if (matchTo != null) {
于 2013-10-27T13:08:28.703 に答える