在外部Android应用程序中读取蓝牙消息

时间:2015-12-04 05:18:31

标签: android bluetooth bluetooth-lowenergy android-bluetooth

我是Android蓝牙的新手,我想使用内部存储或sqlite在外部Android应用程序(我的)中读取和存储蓝牙消息。我已经尝试过GitHub的android bluetooth-chat示例,但我不知道如何实现我的想法。

任何帮助都会有所帮助,谢谢

1 个答案:

答案 0 :(得分:2)

api的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) { } } 部分介绍了蓝牙消息的交换。

http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

以下是管理连接和发送/接收消息的基本示例:

def func(t):
    res = [0] * len(t.time)
    for i, x in enumerate(t.time):
        for y in t.time:
            if abs(x - y) <= 2:
                res[i] += 1
    t['num'] = res
    return t
print(gb.apply(func))

}