如何通过应用程序将树莓派与蓝牙连接

时间:2018-09-08 09:58:50

标签: java bluetooth uuid

我尝试通过蓝牙将应用程序中的数据发送到Raspberry。两者都已连接,我的应用程序获得了设备地址。不幸的是,它没有创建套接字,并且我无法发送任何数据。

我什至不知道错误是否源于设置应用程序或Raspberry错误。希望有人能帮忙...谢谢

代码:

public class MessageThread extends Thread {
    private final static String TAG="MessageThread";
    private final static String MY_UUID ="00001101-0000-1000-8000-00805f9b34fb";
    private BluetoothSocket mSocket=null;
    private String mMessage;


    public MessageThread(BluetoothDevice device, String message) {
        Log.d(TAG,"Trying to send message...");
        this.mMessage=message;
        try {
            UUID uuid = UUID.fromString(MY_UUID);
            mSocket = device.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void manageConnectedSocket(BluetoothSocket socket) throws IOException {
        Log.d(TAG,"Connection successful");
        OutputStream os=socket.getOutputStream();
        PrintStream sender = new PrintStream(os);
        sender.print(mMessage);
        Log.d(TAG,"Message sent");
        InputStream is=socket.getInputStream();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        Log.d(TAG,"Received: " + reader.readLine());
    }

日志:

09-08 11:33:05.937 11589-11937/com.example.paddy.cocktailapp20 D/MessageThread: Trying to send message...
09-08 11:34:14.320 11589-12233/com.example.paddy.cocktailapp20 D/BluetoothAdapter: cancelDiscovery
09-08 11:34:14.333 11589-12233/com.example.paddy.cocktailapp20 D/BluetoothAdapter: cancelDiscovery = true
09-08 11:34:14.343 11589-12233/com.example.paddy.cocktailapp20 D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
09-08 11:34:14.343 11589-12233/com.example.paddy.cocktailapp20 W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
09-08 11:34:14.637 11589-12233/com.example.paddy.cocktailapp20 W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
09-08 11:34:14.639 11589-12233/com.example.paddy.cocktailapp20 W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:821)
09-08 11:34:14.640 11589-12233/com.example.paddy.cocktailapp20 W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:833)
09-08 11:34:14.642 11589-12233/com.example.paddy.cocktailapp20 W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:458)
09-08 11:34:14.643 11589-12233/com.example.paddy.cocktailapp20 W/System.err:     at com.example.paddy.cocktailapp20.MessageThread.run(MessageThread.java:50)

0 个答案:

没有答案
相关问题