Android 2.0上使用蓝牙通信
2010-02-08 00:51:00 来源:WEB开发网1.服务器端:
//请从http://www.uuidgenerator.com上获取你自己的UUID
UUID uuid = uuid.fromString(”27648B4D-D854-5674-FA60E4F535E44AF7″);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord(”MyBluetoothApp”, uuid);
// blocks until a connection is accepted
BluetoothSocket socket = serverSocket.accept();
// close the listening socket ? does not close the connected client socket
serverSocket.close();
2.客户端
// UUID of server socket
UUID uuid = uuid.fromString(“27648B4D-D854-5674-FA60E4F535E44AF7″);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
// BT MAC address of server
BluetoothDevice device = adapter.getRemoteDevice(“00:11:22:33:44:55″);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
adapter.cancelDiscovery();
// blocks until connection established
adapter.connect();
3.双方连接上后,就开始读写了
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(…);
in.read(…);
…
in.close();
out.close();
socket.close();
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接