连接到外部蓝牙设备并开始接收数据

时间:2013-01-11 11:17:42

标签: android

想要连接到设备并开始从外部设备接收数据 现在能够列出设备并与之配对.....现在希望与它联系并开始从中接收数据

Is there any way because i dont exactly know wat kind of data it is sending 
means in which formate etc ...

that is Bluetooth device is continuously sends data to app so the app should continuously receive data

2 个答案:

答案 0 :(得分:3)

使用listenUsingRfcommWithServiceRecord而不是 这个代码在服务中

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class} );
            btSocket = (BluetoothSocket) m.invoke(device, 1);  

答案 1 :(得分:0)

您的问题的答案如下

  

有什么办法,因为我不知道它是什么样的数据   发送意味着甲酸盐等...

是的,有两种方法可以做到这一点

1)使用意图

 Intent i = new Intent(Intent.ACTION_SEND);
 i.setType("image/jpeg");
 i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg")); startActivity(Intent.createChooser(i, "Send Image"));

使用inbuild bluetooth发送数据的代码示例

2)使用使用套接字连接和蓝牙管理器的开源代码通过代码完成。以下是开源代码的链接

blueterm opensource code

通过蓝牙发送的数据是字节数组的形式,您必须解析并将其转换为字符串或您要在代码中使用的任何形式。

蓝牙有两件事

1)配对。

2)全球联系。

如果您使用蓝牙的第一种方法(通过意图)设备将配对但连接将不连续。发送数据连接建立时,其他连接不存在。但是,如果您使用的是blueterm开源代码,那么您的配对和连接是不变的,并且不会破坏用户的需求。

因此,您需要哪种蓝牙功能。

如果我的答案能解决您的问题,请告诉我