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

日志猫

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 com.Test.bluetooth.MainActivity$3@2be07420
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
4

2 回答 2

1

发布您的 logcat 错误。

此外,您为什么要在查看配对设备后关闭整个适配器...记住对 .enable() 和 .disable() 的调用将在用户界面响应之后完成!

永远不要在没有任何用户输入的情况下以编程方式!如果我连接了另一台设备(例如耳机)怎么办?在这里查看更多。

我了解如果未启用适配器,您将无法获得配对的设备,但仍然!

于 2013-02-21T12:03:07.103 回答
1

在您的MainActivitiy::turnOnBt()方法中-这是引发异常的地方,您可以调用Thread.wait();(大概在第 92 行)

我不太确定你试图用这个来实现什么,因为wait()在 UI 线程上调用会(我很确定)让你的应用程序崩溃并出现ANR Exception

要调用Thread.wait();,您必须在同步块中进行:

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

但正如我所说——这不是你想要的。你混过Thread.wait()Thread.sleep(...);?事件然后,调用Thread.sleep(...)UI 线程真的不推荐

于 2013-02-21T13:25:12.330 回答