Как подключить малину через Bluetooth через приложение - PullRequest
0 голосов
/ 08 сентября 2018

Я пытаюсь отправить данные из моего приложения по Bluetooth на 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)
...