WEB开发网
开发学院手机开发Android 开发 Android 2.0上使用蓝牙通信 阅读

Android 2.0上使用蓝牙通信

 2010-02-08 00:51:00 来源:WEB开发网   
核心提示:1.服务器端://请从http://www.uuidgenerator.com上获取你自己的UUIDUUID uuid = uuid.fromString(”27648B4D-D854-5674-FA60E4F535E44AF7″);BluetoothAdapter adapter = BluetoothAdapter

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();

Tags:Android 使用 蓝牙

编辑录入:coldstar [复制链接] [打 印]
赞助商链接