2013-02-21 123 views
0

我有一款應用程序,可以查看手機的配對設備。定期打開和關閉藍牙

我擁有它,以便藍牙打開。但是當我等待時,每當我打開它時,它就會崩潰。

我該怎麼做才能讓它每5分鐘打開一分鐘或類似的東西。只有在沒有連接任何東西的情況下才會關閉。

任何幫助將是偉大的。 由於

代碼

private void turnOnBt() { 
    //Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    //startActivityForResult(intent, 1); 
    BluetoothAdapter.getDefaultAdapter().enable(); 
    try { 
     wait(15000); 
     notify(); 
     BluetoothAdapter.getDefaultAdapter().disable(); 
     wait(60000); 
     notify(); 
     //BluetoothAdapter.getDefaultAdapter().enable(); 
     //Toast.makeText(getApplicationContext(), "Bluetooth restarted", 0).show(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

配對代碼

private void getPairedDevices() { 
    // TODO Auto-generated method stub 
    devicesArray = btAdapter.getBondedDevices(); 
    if(devicesArray.size()>0){ 
     for(BluetoothDevice device:devicesArray){ 
      pairedDevices.add(device.getName()); 
     } 
    } 
} 

logcat的

02-21 13:10:18.559: D/TextLayoutCache(25101): Using debug level: 0 - Debug Enabled: 0 
02-21 13:10:18.589: D/libEGL(25101): loaded /system/lib/egl/libGLES_android.so 
02-21 13:10:18.609: D/libEGL(25101): loaded /system/lib/egl/libEGL_adreno200.so 
02-21 13:10:18.639: D/libEGL(25101): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
02-21 13:10:18.639: D/libEGL(25101): loaded /system/lib/egl/libGLESv2_adreno200.so 
02-21 13:10:19.329: D/OpenGLRenderer(25101): Enabling debug mode 0 
02-21 13:13:07.819: D/TextLayoutCache(25306): Using debug level: 0 - Debug Enabled: 0 
02-21 13:13:07.839: D/libEGL(25306): loaded /system/lib/egl/libGLES_android.so 
02-21 13:13:07.849: D/libEGL(25306): loaded /system/lib/egl/libEGL_adreno200.so 
02-21 13:13:07.869: D/libEGL(25306): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
02-21 13:13:07.869: D/libEGL(25306): loaded /system/lib/egl/libGLESv2_adreno200.so 
02-21 13:13:07.969: D/OpenGLRenderer(25306): Enabling debug mode 0 
02-21 13:16:42.419: D/AndroidRuntime(25750): Shutting down VM 
02-21 13:16:42.419: W/dalvikvm(25750): threadid=1: thread exiting with uncaught exception (group=0x2b542210) 
02-21 13:16:42.439: E/AndroidRuntime(25750): FATAL EXCEPTION: main 
02-21 13:16:42.439: E/AndroidRuntime(25750): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.bluetooth/com.Test.bluetooth.MainActivity}: java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread.access$600(ActivityThread.java:127) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.os.Looper.loop(Looper.java:137) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread.main(ActivityThread.java:4441) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at java.lang.reflect.Method.invokeNative(Native Method) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at java.lang.reflect.Method.invoke(Method.java:511) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at dalvik.system.NativeStart.main(Native Method) 
02-21 13:16:42.439: E/AndroidRuntime(25750): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
02-21 13:16:42.439: E/AndroidRuntime(25750): at java.lang.Object.wait(Native Method) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at java.lang.Object.wait(Object.java:401) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at com.Test.bluetooth.MainActivity.turnOnBt(MainActivity.java:92) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at com.Test.bluetooth.MainActivity.onCreate(MainActivity.java:76) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.Activity.performCreate(Activity.java:4465) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-21 13:16:42.439: E/AndroidRuntime(25750): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
02-21 13:16:42.439: E/AndroidRuntime(25750): ... 11 more 

二logcat的

02-21 13:47:30.079: D/AndroidRuntime(27230): Shutting down VM 
02-21 13:47:30.079: W/dalvikvm(27230): threadid=1: thread exiting with uncaught exception (group=0x2b542210) 
02-21 13:47:30.089: E/AndroidRuntime(27230): FATAL EXCEPTION: main 
02-21 13:47:30.089: E/AndroidRuntime(27230): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in [email protected] 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.os.Handler.handleCallback(Handler.java:605) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.os.Looper.loop(Looper.java:137) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.app.ActivityThread.main(ActivityThread.java:4441) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at java.lang.reflect.Method.invokeNative(Native Method) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at java.lang.reflect.Method.invoke(Method.java:511) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at dalvik.system.NativeStart.main(Native Method) 
02-21 13:47:30.089: E/AndroidRuntime(27230): Caused by: java.lang.NullPointerException 
02-21 13:47:30.089: E/AndroidRuntime(27230): at com.Test.bluetooth.MainActivity$3.onReceive(MainActivity.java:137) 
02-21 13:47:30.089: E/AndroidRuntime(27230): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728) 
02-21 13:47:30.089: E/AndroidRuntime(27230): ... 9 more 

回答

1

安置自己的logcat的錯誤。

此外,爲什麼在查看配對設備之後關閉整個適配器...記住要在用戶界面響應後執行.enable()和.disable()調用!

絕不以編程方式沒有任何用戶輸入!如果我連接了另一臺設備(如耳機),該怎麼辦?查看更多here

據我所知,如果適配器沒有啓用,你不能得到配對的設備,但仍然!

+0

當我回到我的筆記本電腦時,我會將它們發佈。如果沒有可用的配對設備並在5分鐘內再次檢查,我希望它關閉。我用用戶輸入來做,因爲我不需要每次都問。 – ELSheepO 2013-02-21 12:20:58

+0

添加了logcat輸出。 – ELSheepO 2013-02-21 13:13:24

+0

好吧,所以我已經看到了等待的問題,我已經添加了'notify()'代碼,現在它不會崩潰。但它現在不會關閉藍牙。 – ELSheepO 2013-02-21 13:24:49

1

在你MainActivitiy::turnOnBt()方法 - 這是那裏發生異常時,你必須Thread.wait();(presumbly上線92)

我不太清楚你試着用這個來實現,以一個叫什麼通話在UI線程上的wait()(我敢肯定)會使用ANR Exception崩潰你的應用程序。

要調用Thread.wait();你必須做一個synchronized塊:

synchronized(this) { 
    try { 
     Thread.wait(); 
    } 
    catch(InterruptedException e) { 
    } 
} 

但正如我所說 - 這是你想沒有什麼。你有沒有把Thread.wait()Thread.sleep(...);混淆?然後,在UI線程上調用Thread.sleep(...)線程爲really not recommended

+0

我試圖讓它等待,然後關閉藍牙。 92線是我有'wait(15000)'的地方。如果我同步它應該工作的方法是你說什麼? – ELSheepO 2013-02-21 13:34:40