Android应用程序通过蓝牙连接覆盆子pi

时间:2016-06-11 12:39:40

标签: android android-studio bluetooth raspberry-pi broadcastreceiver

我正在尝试将raspberry pi 3与蓝牙连接到智能手机,使用Android来控制传感器。我是Android开发的新手

但是,我无法连接蓝牙。我有蓝牙连接的片段,如下所示。当我打开应用程序并打开蓝牙时,我能够看到可用设备的列表,但是当我尝试连接时,它没有连接。我没有收到任何错误。请帮我。感谢

private class ConnectThread extends Thread {

        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;
            Log.i(TAG, "construct");
            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.i(TAG, "get socket failed");

            }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            bluetoothAdapter.cancelDiscovery();
            Log.i(TAG, "connect - run");
            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
                Log.i(TAG, "connect - succeeded");
            } catch (IOException connectException) {    Log.i(TAG, "connect failed");
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }

            // Do work to manage the connection (in a separate thread)

            mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
        }



        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }

logcat的:

06-11 12:42:42.366 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: onActivityCreated
06-11 12:42:42.368 24775-24775/pratik.prajapati.rpicontrol D/BluetoothAdapter: 592452976: getState() :  mService = null. Returning STATE_OFF
06-11 12:42:42.368 24775-24775/pratik.prajapati.rpicontrol D/BluetoothAdapter: 592452976: getState() :  mService = null. Returning STATE_OFF
06-11 12:42:44.242 24775-24775/pratik.prajapati.rpicontrol D/BluetoothAdapter: 592452976: getState() :  mService = null. Returning STATE_OFF
06-11 12:42:44.242 24775-24775/pratik.prajapati.rpicontrol D/BluetoothAdapter: 592452976: getState() :  mService = null. Returning STATE_OFF
06-11 12:42:44.569 24775-24775/pratik.prajapati.rpicontrol W/InputMethodManager: startInputInner : InputBindResult == null
06-11 12:42:46.671 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: BroadcastReceiver
06-11 12:42:46.672 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: ACTION_DISCOVERY_STARTED
06-11 12:42:46.672 24775-24775/pratik.prajapati.rpicontrol I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1f4b7828 time:54694433
06-11 12:42:54.474 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: BroadcastReceiver
06-11 12:42:54.476 24775-24775/pratik.prajapati.rpicontrol D/BluetoothDevice: getService : Register callback
06-11 12:42:54.481 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: New Device Found : WASI (UnPaired)
                                                                                                                         60:36:DD:56:00:44
06-11 12:43:12.396 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: BroadcastReceiver
06-11 12:43:12.400 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: New Device Found : DESKTOP-36AH3D9 (UnPaired)
                                                                                                                         60:6C:66:8B:B7:57
06-11 12:43:13.228 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: BroadcastReceiver
06-11 12:43:13.229 24775-24775/pratik.prajapati.rpicontrol D/Bluetooth_Fragment : Debug ==============================>: ACTION_DISCOVERY_FINISHED
06-11 12:43:21.046 24775-24775/pratik.prajapati.rpicontrol I/Bluetooth_Fragment : Debug ==============================>: construct
06-11 12:43:21.054 24775-26477/pratik.prajapati.rpicontrol I/Bluetooth_Fragment : Debug ==============================>: connect - run
06-11 12:43:21.054 24775-26477/pratik.prajapati.rpicontrol W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
06-11 12:43:33.273 24775-26477/pratik.prajapati.rpicontrol I/Bluetooth_Fragment : Debug ==============================>: connect failed 

please find project here

0 个答案:

没有答案