ftp上传工具下载,美丽家园歌词,滨海论坛网
1. 使用蓝牙的响应权限
在这里首先要了解对蓝牙操作一个核心类bluetoothadapter
使用bluetoothadapter的startdiscovery()方法来搜索蓝牙设备
startdiscovery()方法是一个异步方法,调用后会立即返回。该方法会进行对其他蓝牙设备的搜索,该过程会持续12秒。该方法调用后,搜索过程实际上是在一个system service中进行的,所以可以调用canceldiscovery()方法来停止搜索(该方法可以在未执行discovery请求时调用)。
请求discovery后,系统开始搜索蓝牙设备,在这个过程中,系统会发送以下三个广播:
action_discovery_start:开始搜索
action_discovery_finished:搜索结束
action_found:找到设备,这个intent中包含两个extra fields:extra_device和extra_class,分别包含bluetoodevice和bluetoothclass。
我们可以自己注册相应的broadcastreceiver来接收响应的广播,以便实现某些功能
如果打算建议两个蓝牙设备之间的连接,则必须实现服务器端与客户端的机制。当两个设备在同一个rfcomm channel下分别拥有一个连接的bluetoothsocket,这两个设备才可以说是建立了连接。
服务器设备与客户端设备获取bluetoothsocket的途径是不同的。服务器设备是通过accepted一个incoming connection来获取的,而客户端设备则是通过打开一个到服务器的rfcomm channel来获取的。
服务器端的实现
通过调用bluetoothadapter的listenusingrfcommwithservicerecord(string, uuid)方法来获取bluetoothserversocket(uuid用于客户端与服务器端之间的配对)
调用bluetoothserversocket的accept()方法监听连接请求,如果收到请求,则返回一个bluetoothsocket实例(此方法为block方法,应置于新线程中)
如果不想在accept其他的连接,则调用bluetoothserversocket的close()方法释放资源(调用该方法后,之前获得的bluetoothsocket实例并没有close。但由于rfcomm一个时刻只允许在一条channel中有一个连接,则一般在accept一个连接后,便close掉bluetoothserversocket)
public acceptthread() {
// use a temporary object that is later assigned to mmserversocket,
// because mmserversocket is final
bluetoothserversocket tmp = null;
try {
// my_uuid is the app's uuid string, also used by the client code
tmp = mbluetoothadapter.listenusingrfcommwithservicerecord(name, my_uuid);
} catch (ioexception e) { }
mmserversocket = tmp;
}
public void run() {
bluetoothsocket socket = null;
// keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmserversocket.accept();
} catch (ioexception e) {
break;
}
// if a connection was accepted
if (socket != null) {
// do work to manage the connection (in a separate thread)
manageconnectedsocket(socket);
mmserversocket.close();
break;
}
}
}
/** will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmserversocket.close();
} catch (ioexception e) { }
}
}
调用bluetoothservice的listenusingrfcommwithservicerecord(string, uuid)方法获取bluetoothsocket(该uuid应该同于服务器端的uuid)
调用bluetoothsocket的connect()方法(该方法为block方法),如果uuid同服务器端的uuid匹配,并且连接被服务器端accept,则connect()方法返回
注意:在调用connect()方法之前,应当确定当前没有搜索设备,否则连接会变得非常慢并且容易失败
public connectthread(bluetoothdevice device) {
// use a temporary object that is later assigned to mmsocket,
// because mmsocket is final
bluetoothsocket tmp = null;
mmdevice = device;
// get a bluetoothsocket to connect with the given bluetoothdevice
try {
// my_uuid is the app's uuid string, also used by the server code
tmp = device.createrfcommsockettoservicerecord(my_uuid);
} catch (ioexception e) { }
mmsocket = tmp;
}
public void run() {
// cancel discovery because it will slow down the connection
mbluetoothadapter.canceldiscovery();
try {
// connect the device through the socket. this will block
// until it succeeds or throws an exception
mmsocket.connect();
} catch (ioexception connectexception) {
// unable to connect; close the socket and get out
try {
mmsocket.close();
} catch (ioexception closeexception) { }
return;
}
// do work to manage the connection (in a separate thread)
manageconnectedsocket(mmsocket);
}
/** will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmsocket.close();
} catch (ioexception e) { }
}
}
使用read(bytes[])和write(bytes[])方法分别进行读写操作
注意:read(bytes[])方法会一直block,知道从流中读取到信息,而write(bytes[])方法并不是经常的block(比如在另一设备没有及时read或者中间缓冲区已满的情况下,write方法会block)
public connectedthread(bluetoothsocket socket) {
mmsocket = socket;
inputstream tmpin = null;
outputstream tmpout = null;
// get the input and output streams, using temp objects because
// member streams are final
try {
tmpin = socket.getinputstream();
tmpout = socket.getoutputstream();
} catch (ioexception e) { }
mminstream = tmpin;
mmoutstream = tmpout;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// keep listening to the inputstream until an exception occurs
while (true) {
try {
// read from the inputstream
bytes = mminstream.read(buffer);
// send the obtained bytes to the ui activity
mhandler.obtainmessage(message_read, bytes, -1, buffer)
.sendtotarget();
} catch (ioexception e) {
break;
}
}
}
/* call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmoutstream.write(bytes);
} catch (ioexception e) { }
}
/* call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmsocket.close();
} catch (ioexception e) { }
}
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论