Android蓝牙RFCOMM连接套接字无法连接

时间:2017-05-25 20:54:40

标签: android sockets bluetooth rfcomm

我正在尝试编写一个Android应用程序来连接我的蓝牙服务器所在的覆盆子。如果它改变了问题,覆盆子上的蓝牙有一个A2DP配置文件,设备是配对的。我想建立一个RFCOMM连接来向raspberry发送命令。

以下是连接的代码:

public class ConnectBluetooth extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    static final UUID uuid = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");

    public ConnectBluetooth(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket
        // because mmSocket is final.
        BluetoothSocket tmp = null;
        mmDevice = device;

        try {
            // Get a BluetoothSocket to connect with the given BluetoothDevice.
            // MY_UUID is the app's UUID string, also used in the server code.
            tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Log.e("BluetoothC", "Socket's create() method failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it otherwise slows down the connection.
      //  mBluetoothAdapter.

        try {
            // Connect to the remote device through the socket. This call blocks
            // until it succeeds or throws an exception.
            mmSocket.connect();
        } catch (IOException connectException) {
            Log.e("BluetoothC", "Could not connect");
            try {
                mmSocket.close();
            } catch (IOException closeException) {
             Log.e("BluetoothC", "Could not close the client socket", closeException);
            }
            return;
        }

我已经尝试了几个UUID,getUUIDS方法为设备返回。

以下是我使用该类的代码:

 final Button button = (Button) findViewById(R.id.button9);
        button.setOnClickListener(new View.OnClickListener()

        {
            public void onClick (View view){
                Toast.makeText(getApplicationContext(),
                        "Button pressed", Toast.LENGTH_LONG)
                        .show();
                BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                BluetoothDevice deviceBlth=null;
                if (mBluetoothAdapter == null) {
                    Toast.makeText(getApplicationContext(),
                            "Device does not suppport Blth", Toast.LENGTH_LONG)
                            .show();
                                   }
                if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                }
                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

                if (pairedDevices.size() > 0) {
                    // There are paired devices. Get the name and address of each paired device.
                    for (BluetoothDevice device : pairedDevices) {
                        if(Objects.equals(device.getName(), "raspberrypi")){

                            { Toast.makeText(getApplicationContext(),
                                    "Raspberry  found", Toast.LENGTH_LONG)
                                    .show();}
                        String deviceName = device.getName();
                        String deviceHardwareAddress = device.getAddress(); // MAC address
                        //Add UUID here
                         String deviceUUID=device.getUuids()[0].toString();
                            Log.w("Bluetooth",deviceUUID);
                            Log.w("Bluetooth",device.getUuids()[1].toString());
                            Log.w("Bluetooth",device.getUuids()[2].toString());
                            Toast.makeText(getApplicationContext(),
                                    deviceUUID, Toast.LENGTH_LONG)
                                    .show();
                            deviceBlth=device;
                            break;
                    }
                    else
                        { Toast.makeText(getApplicationContext(),
                                "Raspberry not found", Toast.LENGTH_LONG)
                                .show();}
                }
            }

                Intent discoverableIntent =
                        new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                startActivity(discoverableIntent);


                Toast.makeText(getApplicationContext(),
                       deviceBlth.getName(), Toast.LENGTH_LONG)
                        .show();
            ConnectBluetooth connectB= new ConnectBluetooth(deviceBlth);
                mBluetoothAdapter.cancelDiscovery();
                connectB.run();

        }
    });

这是运行应用程序时的日志:

05-25 23:44:41.438: W/Bluetooth(8822): 0000110e-0000-1000-8000-00805f9b34fb
05-25 23:44:41.488: D/BluetoothAdapter(8822): cancelDiscovery
05-25 23:44:41.488: D/BluetoothAdapterProperties(5415): mDiscovering is false
05-25 23:44:41.488: E/BluetoothAdapterService(5415): This is not a scanning status. cancelDiscovery() will be not called.
05-25 23:44:41.488: D/BluetoothAdapter(8822): cancelDiscovery = true
05-25 23:44:41.498: D/BluetoothUtils(8822): isSocketAllowedBySecurityPolicy start : device null
05-25 23:44:41.498: D/BluetoothSocket(8822): connect(): myUserId = 0
05-25 23:44:41.498: W/BluetoothAdapter(8822): getBluetoothService() called with no BluetoothManagerCallback
05-25 23:44:42.298: E/BluetoothRemoteDevices(5415): aclStateChangeCallback: State:Connected to Device:B8:27:EB:58:45:XX, linktype is 1
05-25 23:44:42.298: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:42.308: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:42.308: V/BluetoothEventManager(2323): onReceive :: android.bluetooth.device.action.NAME_CHANGED
05-25 23:44:42.308: V/BluetoothEventManager(5536): onReceive :: android.bluetooth.device.action.NAME_CHANGED
05-25 23:44:42.318: E/BluetoothC(8822): Could not connect
05-25 23:44:42.348: D/BluetoothTile(2323): handleUpdateState :  supported = true, enabled = true, enabling = false, connected = false, connecting = false, mController.getLastDeviceName() = null
05-25 23:44:43.378: D/BluetoothAdapter(5536): setScanMode() Mode :23, duration : 300
05-25 23:44:43.388: D/BluetoothAdapterProperties(5415): Discoverable Timeout:300
05-25 23:44:43.388: D/BluetoothDiscoverableTimeoutReceiver(5536): setDiscoverableAlarm(): alarmTime = 1495745383396
05-25 23:44:43.388: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:43.388: D/BluetoothDiscoverableTimeoutReceiver(5536): setDiscoverableAlarm(): cancel prev alarm
05-25 23:44:43.388: D/BluetoothAdapterProperties(5415): Scan Mode:23
05-25 23:44:43.398: D/BluetoothUtils(5415): makeManufacturerData :: 
05-25 23:44:43.418: E/BluetoothServiceJni(5415): adapter_properties_callback: Status 1 is incorrect
05-25 23:44:46.388: E/BluetoothRemoteDevices(5415): aclStateChangeCallback: State:DisConnected to Device:B8:27:EB:58:45:XX
05-25 23:44:46.388: V/BluetoothEventManager(2323): onReceive :: android.bluetooth.device.action.ACL_DISCONNECTED
05-25 23:44:46.388: E/BluetoothEventManager(2323): ACTION_ACL_DISCONNECTED
05-25 23:44:46.388: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:46.388: D/BluetoothDataManager(5415): BluetoothDataManager :: notifyDisconnectReason() is called!!!
05-25 23:44:46.388: D/BluetoothAdapterService(5415): getAdapterService() - returning com.android.bluetooth.btservice.AdapterService@2365ebc
05-25 23:44:46.398: V/BluetoothEventManager(5536): onReceive :: android.bluetooth.device.action.ACL_DISCONNECTED
05-25 23:44:46.398: E/BluetoothEventManager(5536): ACTION_ACL_DISCONNECTED
05-25 23:44:46.398: D/BluetoothAdapterService(5415): getAdapterService() - returning com.android.bluetooth.btservice.AdapterService@2365ebc
05-25 23:44:46.408: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:46.408: V/BluetoothEventManager(5536): onReceive :: android.bluetooth.device.action.NAME_CHANGED
05-25 23:44:46.408: V/BluetoothEventManager(2323): onReceive :: android.bluetooth.device.action.NAME_CHANGED
05-25 23:44:46.408: D/BluetoothUtils(5415): getBtEnabledContainers(): btContainers = []
05-25 23:44:46.408: E/BluetoothRemoteDevices(5415): devicePropertyChangedCallback: bdDevice: B8:27:EB:58:45:6F, value is empty for type: 241
05-25 23:44:46.408: I/BluetoothPbapService(5415): action: android.bluetooth.device.action.ACL_DISCONNECTED, state: -2147483648
05-25 23:44:46.408: E/BluetoothPbapService(5415): Device  B8:27:EB:58:45:6F not waiting for authorization..Skipping cancel dialog...
05-25 23:44:46.428: V/BluetoothEventManager(5536): onReceive :: android.bluetooth.device.action.CLASS_CHANGED
05-25 23:44:46.438: V/BluetoothEventManager(2323): onReceive :: android.bluetooth.device.action.CLASS_CHANGED
05-25 23:44:46.438: D/BluetoothTile(2323): handleUpdateState :  supported = true, enabled = true, enabling = false, connected = false, connecting = false, mController.getLastDeviceName() = null
05-25 23:44:46.898: D/BluetoothAdapterService(5415): getAdapterService() - returning com.android.bluetooth.btservice.AdapterService@2365ebc

有人可以指出我在哪里做错了吗?

0 个答案:

没有答案