蓝牙未在 4.4.2 上连接

2022-09-01 21:29:16

我有一个蓝牙设备,它已连接到我在4.4.2之前尝试过的所有Android版本上。现在,它没有在Galaxy Tab 4或S3上连接。选项卡 3 与 4.1.2 连接正常。在尝试初始化 .我使用的代码基于 sdk 中的聊天示例。AcceptThreadBluetoothSocket

我的接受代码

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()以上是我返回绑定设备地址的方法。

错误

在 中,如果我使用run()

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

然后我在Android框架中得到一个501行,这是这条线NPEBluetoothSocket.java

int left = b.length;

所以看起来是这样,但它似乎不是在调试之后。 是一个从我的类发送的,它发送一个全新的,从内部初始化,就像sdk示例一样,就像我在旧版本上所做的那样。bnullbbyte[]BluetoothServicebyte[]ConnectedThread

如果我注释掉并尝试使用

socket = mmServerSocket.accept();

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

bt socket is not in listen state

所以,我不知道如何把它放在“倾听状态”,或者为什么它不会。有没有人经历过这种情况或知道如何解决它?或者如何防止获取如果我使用第一个片段(如果这是正确的)。NPE

我发现

当我得到一个我发现这篇文章,把我带到这里,但这并没有让我去任何地方。IOException

通知:赏金消息显示 4.4.4,但在 Tab 4 上显示为 4.4.2

设备错误

当我第一次通过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 ***********************

但我无法找出这面旗帜的含义。

我意识到BT堆栈4.x中存在已知的错误(请参阅许多错误报告之一)

minSDK目前为 10。但是,如果我找到一个可行的解决方案,那么我可以解决这个问题。


答案 1

试试这个代码,这是在nexus 7上工作的Android 4.4.2

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;

}


答案 2

我也使用过使用相同代码的连接步骤,但这仅适用于智能Android设备。

您可以获取要连接的特定设备的蓝牙设备对象。之后,您可以使用蓝牙头集的连接和断开方法通过反射连接到可识别的蓝牙设备。

Class<?> BTHeadset = Class.forName("android.bluetooth.BluetoothHeadset");
        Method conn = BTHeadset.getMethod("connect", new Class[] {BluetoothDevice.class});
               conn.invoke(BTHeadset, new Object[] {btDev});

您可以使用相同的代码,通过反射使用断开方法来分解设备。


推荐