两个安卓设备之间的蓝牙数据传输

11

我一直在按照这个 Android 指南进行蓝牙通信。

为了确切地解释我的目标,当两个设备配对成功时,每个设备上都会打开两个不同的活动(服务器和客户端),在服务器活动上有不同的按钮,在客户端活动上只有一个文本视图。 我想能够在服务器设备上按下一个按钮并在客户端上显示它。

我已经成功建立了两个设备之间的连接,但现在我想发送数据,但我还没有成功。

他们提供了以下代码用于数据传输:

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}

public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.read(buffer);
            // Send the obtained bytes to the UI activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            break;
        }
    }
}

/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}
}

但是这行代码会产生一个错误

// Send the obtained bytes to the UI activity
            mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

在指南中没有解释,我不知道mHandler是什么或它的作用。

除了错误之外,我甚至不知道应该把这段代码放在哪里。它应该放在我打开的第二个活动(服务器和客户端)中还是主要活动中?如果放在Server活动中,它应该在所有按钮的onClick方法中,每个按钮发送不同的字节码?在这段代码中,我们如何区分谁是发送方,谁是接收方?


你在manifest.xml文件中添加了适当的权限吗?? <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> - addy123
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 或者 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> - ban-geoengineering
5个回答

13

请查看SDK中Google提供的蓝牙聊天示例,它将向您展示如何在蓝牙上实现基本文本发送。


这个答案中的链接非常老旧。这里有一个更新的官方蓝牙聊天应用程序的示例:https://github.com/googlesamples/android-BluetoothChat - ban-geoengineering

1

您也可以尝试这里的教程示例here


0
// Enter code here

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        byte[] writeBuf = (byte[]) msg.obj;
        int begin = (int)msg.arg1;
        int end = (int)msg.arg2;

        switch(msg.what) {
            case 1:
                String writeMessage = new String(writeBuf);
                writeMessage = writeMessage.substring(begin, end);
                break;
        }
    }
};

0

mHandler用于从BluetoothHandle.java向您的Activity传递消息。这将帮助您更新屏幕上由BluetoothHandler返回的消息。

您必须从您的Activity中创建mHandler并像这样调用您的处理程序 -

mBluetoothHandler = new BluetoothHandler(this, mHandler);

您的BluetoothHandler.java具有如下构造函数 -

public class BluetoothHandler { 

    public BluetoothHandler(Context context, Handler handler) {
            mAdapter = BluetoothAdapter.getDefaultAdapter();
            mState = STATE_NONE;
            mHandler = handler;
            mcontext = context;
   }

}

更多详情,请参考蓝牙聊天的Android示例项目。 您也可以使用此链接: http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html


问题中的链接无法使用,但您可以在此找到(当前的)官方BluetoothChat示例应用程序:https://github.com/googlesamples/android-BluetoothChat - ban-geoengineering

0

请问您能描述一下您所看到的错误吗?

根据Ankit和Addy的信息,BlueToothChat是您参考的最佳代码。通过在两个Android设备上加载它来进行实验-使用一个作为服务器,另一个作为客户端,在它们之间交换消息。这样的实验将帮助您了解其代码并决定您的编码逻辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接