今天我正在使用蓝牙,现在我可以打开它并连接到我的机器人,但我找不到如何向我的机器人发送一封信(如“w”)。一些示例代码会是什么样的呢?
所以,我再试一次。单击按钮时应用程序失败。
private OutputStream outStream = null;
private BluetoothSocket btSocket = null;
private BluetoothDevice device = null;
static String address = "00:12:02:09:05:21";
private static final UUID MY_UUID =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
BluetoothAdapter myBTadapter;
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
device = myBTadapter.getRemoteDevice(address);
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
}
try
{
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch (Exception e) {
}
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
}
}
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
}
char example = 'w';
outStream.write(example);
outStream.flush();
} catch (IOException e) {
}
}
});
像这样......:)