gpt4 book ai didi

java - 检查连接的蓝牙设备状态

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:48 25 4
gpt4 key购买 nike

如何在传递数据之前知道蓝牙设备是否仍然连接到笔记本电脑?我使用 blue-cove 编写了一个 java 程序lib 通过笔记本电脑发现和传递数据。第一次传数据后,如何在下次传数据前检查设备是否还连接着笔记本电脑?

我看到了类似的问题。但是他们问了如何查看连接到android的蓝牙设备状态。

最佳答案

您可以按照一系列步骤创建一个异步任务来验证连接,同时在设备之间搜索和共享文件。

函数 deviceDiscovered()inquiryCompleted() 是在发现设备和查询完成时执行的函数。

如果设备断开连接,您将从 inqueryCompleted 方法收到通知并在行中处理它:

try {
synchronized(lock){
lock.wait();
}
}

catch (InterruptedException e) {
e.printStackTrace();
}

InterruptedException中你可以处理这个错误。

package bt;

import java.io.OutputStream;
import java.util.ArrayList;

import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;

import bt.BTListener.MyDeviceListenerFilter;

public class MyDiscoveryListener implements DiscoveryListener{

private static Object lock=new Object();
public ArrayList<RemoteDevice> devices;

public MyDiscoveryListener() {
devices = new ArrayList<RemoteDevice>();
}

public static void main(String[] args) {

MyDiscoveryListener listener = new MyDiscoveryListener();

try{
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, listener);

try {
synchronized(lock){
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
return;
}


System.out.println("Device Inquiry Completed. ");


UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID(0x1105); //OBEX Object Push service

int[] attrIDs = new int[] {
0x0100 // Service name
};

for (RemoteDevice device : listener.devices) {
agent.searchServices(
attrIDs,uuidSet,device,listener);


try {
synchronized(lock){
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
return;
}


System.out.println("Service search finished.");
}

} catch (Exception e) {
e.printStackTrace();
}
}



@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
String name;
try {
name = btDevice.getFriendlyName(false);
} catch (Exception e) {
name = btDevice.getBluetoothAddress();
}

devices.add(btDevice);
System.out.println("device found: " + name);

}

@Override
public void inquiryCompleted(int arg0) {
synchronized(lock){
lock.notify();
}
}

@Override
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized (lock) {
lock.notify();
}
}

@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (url == null) {
continue;
}
DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
if (serviceName != null) {
System.out.println("service " + serviceName.getValue() + " found " + url);

if(serviceName.getValue().equals("OBEX Object Push")){
sendMessageToDevice(url);
}
} else {
System.out.println("service found " + url);
}


}
}

private static void sendMessageToDevice(String serverURL){
try{
System.out.println("Connecting to " + serverURL);

ClientSession clientSession = (ClientSession) Connector.open(serverURL);
HeaderSet hsConnectReply = clientSession.connect(null);
if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
System.out.println("Failed to connect");
return;
}

HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
hsOperation.setHeader(HeaderSet.TYPE, "text");

//Create PUT Operation
Operation putOperation = clientSession.put(hsOperation);

// Send some text to server
byte data[] = "Hello World !!!".getBytes("iso-8859-1");
OutputStream os = putOperation.openOutputStream();
os.write(data);
os.close();

putOperation.close();

clientSession.disconnect(null);

clientSession.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

}

关于java - 检查连接的蓝牙设备状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com