蓝牙无法连接 4.4.2 [英] Bluetooth not connecting on 4.4.2

查看:52
本文介绍了蓝牙无法连接 4.4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个蓝牙设备,它可以连接到我在 4.4.2 之前尝试过的所有 Android 版本上.现在,它无法连接到 Galaxy Tab 4 或 S3.表 3 与 4.1.2 连接良好.在尝试初始化 BluetoothSocket 时,问题似乎发生在 AcceptThread 中.我使用的代码基于 sdk 中的聊天示例.

I've got a Bluetooth device that has connected on all versions of Android that I have tried prior to 4.4.2. Now, it's not connecting on the Galaxy Tab 4 or the S3. The Tab 3 connects fine with 4.1.2. The problem seems to occur in the AcceptThread while trying to initialize the BluetoothSocket. The code I'm using is based off of the chat example in the sdk.

我的接受代码

private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;
    public boolean successInit = false;

    public AcceptThread() {
        closeAllConnections();          
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        while(!successInit) {
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                successInit= true;
                Log.i("TAG", "Socket Server Created");
            }
            catch(Exception e) {
                Log.i("TAG", e.getMessage());
                successInit = false;
            }
        }

        mmServerSocket = tmp;
    }
    public void run() {
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        Log.i("BluetoothComService", String.valueOf(mState));
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mAdapter.cancelDiscovery();

                socket = BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

           //   socket = mmServerSocket.accept();  // here socket might be closed or timeout
                Log.e("BluetoothComService", "No accept Exception");
            }catch (IOException e) {
                Log.e("TAG", e.getMessage());
            }

getThermMAC() 上面是我返回绑定设备地址的方法.

getThermMAC() above is my method to return the address of the bonded device.

run()中,如果我使用

socket=BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

然后我在Android框架中的BluetoothSocket.java的第501行得到一个NPE

then I get a NPE at line 501 of BluetoothSocket.java in the Android framework which is this line

int left = b.length;

所以看起来 bnull 但它在调试后似乎不是.b 是一个 byte[],它是从我的 BluetoothService 类发送的,它发送一个全新的、初始化的 byte[]ConnectedThread 中,就像 sdk 示例和我在旧版本上所做的一样.

So it would appear that b is null but it doesn't appear to be after debugging. b is a byte[] which is sent from my BluetoothService class which sends a brand new, initialized byte[] from within the ConnectedThread just as the sdk example does and as I've been doing on older versions.

如果我将其注释掉并尝试使用

If I comment that out and try to use

socket = mmServerSocket.accept();

过去一直在工作,然后我得到

which has been working in the past, then I get

bt socket is not in listen state

所以,我不知道如何将其置于侦听状态"或为什么不会.有没有人经历过这个或知道如何解决它?或者,如果我使用第一个片段(如果那是正确的),如何避免获得 NPE.

So, I don't know how to put it in "listen state" or why it wouldn't be. Has anyone experienced this or know how to workaround it? Or how to keep from getting the NPE if I use the first snippet (if that is even correct).

我找到了

当我收到 IOException 时,我发现了 这篇文章 这让我这里 但这还没有得到我在任何地方.

When I was getting an IOException I found this post which led me here but this hasn't gotten me anywhere.

注意:赏金信息显示为 4.4.4,但在选项卡 4 上为 4.4.2

Notice: The bounty message says 4.4.4 but it is 4.4.2 on the Tab 4

设备错误

当我第一次通过 USB 将设备连接到计算机时,我也注意到这些蓝牙错误

I also notice these Bluetooth errors when I first connect my device to the computer through USB

09-05 15:18:03.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.177: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************

但我一直无法找出那个标志的含义.

but I have not been able to find out what that flag means.

我意识到 BT 堆栈 4.x 中存在已知错误 (查看众多错误报告之一)

I realize there are known bugs in the BT stack 4.x (See one of many bug reports)

minSDK 目前是 10.不过,如果我找到了一个可行的解决方案,那么我可以解决这个问题.

minSDK is currently 10. Though, if I find a working solution then I can work around that.

推荐答案

试试这个代码,这个在nexus 7上运行android 4.4.2

Try this code, this is working android 4.4.2 on nexus 7

private boolean refreshDeviceCache(BluetoothGatt gatt){
    try {
        BluetoothGatt localBluetoothGatt = gatt;
        Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
        if (localMethod != null) {
           boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
            return bool;
         }
    } 
    catch (Exception localException) {
        Log.e(TAG, "An exception occured while refreshing device");
    }
    return false;
}


    public boolean connect(final String address) {
           if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
                return false;
        }
            // Previously connected device. Try to reconnect.
            if (mBluetoothGatt != null) {
                Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
              if (mBluetoothGatt.connect()) {
                    return true;
               } else {
                return false;
               }
        }

        final BluetoothDevice device = mBluetoothAdapter
                .getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }

        // We want to directly connect to the device, so we are setting the
        // autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
        refreshDeviceCache(mBluetoothGatt);
        Log.d(TAG, "Trying to create a new connection.");
        return true;

}

这篇关于蓝牙无法连接 4.4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆