简体   繁体   English

将Android与PC连接并显示消息

[英]Connecting Android with PC and displaying a message

I am new to android. 我是Android新手。 I am just trying to connect my Android device to PC and pass a string to PC using Bluetooth. 我只是想将我的Android设备连接到PC,然后使用蓝牙将字符串传递给PC。 I have no idea on how to do it. 我不知道该怎么做。 Android side I read about the Bluetooth API. 我在Android方面了解了蓝牙API。 Please suggest me some ways to do it. 请给我建议一些方法。 Thanks in advance. 提前致谢。

For Android, my code is slightly different from yours: 对于Android,我的代码与您的代码略有不同:

BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

dos.writeChar('x'); // for example

socket.close();

I used DataOutputStream to send data to PC. 我使用DataOutputStream将数据发送到PC。 But surely this doesn't matter, just for your reference. 但这当然没关系,仅供参考。

For PC, 对于PC,

LocalDevice localDevice = LocalDevice.getLocalDevice();

localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service

String url = "btspp://localhost:" + device_UUID + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);

StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();

char c;
while (true) {
    c = dis.readChar();
    if (c == 'x')
    break;
}

 connection.close();

I am not sure if the above codes still work today, as this was done 2 years ago. 我不确定上述代码是否仍然可以像2年前那样在今天使用。 The BlueCove API may have changed a lot. BlueCove API可能已发生很大变化。 But anyway, these codes work for me. 但是无论如何,这些代码对我有用。 Hope this may help you. 希望这对您有帮助。

One more note is that, I had to uninstall the Toshiba Bluetooth Driver in my PC and reinstall the Microsoft one in order to make use of BlueCove. 还有一点需要注意的是,为了使用BlueCove,我必须在PC上卸载Toshiba Bluetooth Driver,然后重新安装Microsoft。 Otherwise, it won't work. 否则,它将无法正常工作。 (However, latest version of BlueCove may have already supported different drivers, please correct me if I said anything wrong.) (但是,最新版本的BlueCove可能已经支持不同的驱动程序,如果我说错了,请纠正我。)

( Author: Victor Wong ) 作者:黄宗宪

For clarification: on the PC side, you usually have a bluetooth device that comes with a virtual COM port. 为了澄清起见:在PC端,您通常有一个带有虚拟COM端口的蓝牙设备。 For testing purposes, you can use any terminal program (eg http://realterm.sourceforge.net/ ). 出于测试目的,您可以使用任何终端程序(例如http://realterm.sourceforge.net/ )。 When you start it on your virtual bluetooth serial port and connect your Android device, it will show the received data. 当您在虚拟蓝牙串行端口上启动它并连接Android设备时,它将显示收到的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM