我一直在尝试使用蓝牙RFCOMM连接Google Glass和Nexus设备,其中在Nexus设备上运行的应用程序充当服务器,并且The Glass尝试将客户端连接到同一个UUID。
答案:https://stackoverflow.com/posts/20414642/非常有用,虽然我无法获得所需的功能。连接部分没有问题,但基本上我想a)将一些数据传输到Android设备b)获取对Glass的响应。所以这是我想要的双向沟通。
我遵循了Android开发者网站上的蓝牙指南,我认为处理程序用于管理现有连接的方式需要进行修改。
Glass App上的处理程序
public Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(TAG, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
//read and write data from remote device
unregisterReceiver(mReceiver);
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
connectedThread.start();
Toast.makeText(getApplicationContext(), "CONNECTED", 2).show();
//setContentView(R.layout.activity_main);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
int bufferContent = ByteBuffer.wrap(readBuf).getInt();
String string = new String(readBuf);
break;
}
}
};
在Glass App上运行的ConnectThread的run()函数
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.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, the handler will send the connecting successful message with the socket back to the pool
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
// manage connected socket
// manageConnectedSocket(mmSocket);
}
套接字管理在Android设备上运行的连接
private void manageConnectedSocket(BluetoothSocket socket) {
// start our connection thread
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
// so the HH can show you it's working and stuff...
String devs = "";
for (BluetoothSocket sock : mSockets) {
devs += sock.getRemoteDevice().getName() + "\n";
}
// pass it to the pool....
Message msg = handle.obtainMessage(STATE_CONNECTION_STARTED);
Bundle bundle = new Bundle();
bundle.putString("NAMES", devs);
msg.setData(bundle);
handle.sendMessage(msg);
}
方法的命名方案遵循官方文档中提到的方案 - http://developer.android.com/guide/topics/connectivity/bluetooth.html
我的问题是,这些方法/或任何其他允许将TextView的文本从Glass传输到Nexus设备的textView并返回的任何其他方法需要进行哪些更改。