При компиляции этого кода возникает несколько проблем.
(http://www.aviyehuda.com/blog /2010/01/08/connecting-to-bluetooth-devices-with-java/)
Эти исключения возникают только при подключении Samsung Galaxy S10 и ZFlip 4.
Однако, при использовании Galaxy s8 нет исключений.
Пожалуйста, дайте мне решение этой проблемы.
Спасибо.
(Приношу извинения за свои знания английского

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;
public class OfficialTest implements DiscoveryListener{
private static Object lock=new Object();
public ArrayList devices;
public OfficialTest() {
devices = new ArrayList();
}
public static void main(String[] args) {
OfficialTest listener = new OfficialTest();
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.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (url == null) {
continue;
}
DataElement serviceName = servRecord.getAttributeValue(0x0100);
if (serviceName != null) {
System.out.println("service " + serviceName.getValue() + " found " + url);
if(serviceName.getValue().equals("OBEX Object Push\0")){
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();
}
}
}
Происходят некоторые ошибки. Всегда существует IOException или EOFException.
Вот код ошибки.
Connecting to btgoep://A82BB9E0ED8B:12;authenticate=false;encrypt=false;master=false
java.io.IOException:
at com.intel.bluetooth.BluetoothStackMicrosoft.send(Native Method)
at com.intel.bluetooth.BluetoothStackMicrosoft.connectionRfWrite(BluetoothStackMicrosoft.java:829)
at com.intel.bluetooth.BluetoothRFCommOutputStream.write(BluetoothRFCommOutputStream.java:84)
at java.base/java.io.OutputStream.write(OutputStream.java:124)
at com.intel.bluetooth.obex.OBEXSessionBase.writePacketWithFlags(OBEXSessionBase.java:183)
at com.intel.bluetooth.obex.OBEXSessionBase.writePacket(OBEXSessionBase.java:147)
at com.intel.bluetooth.obex.OBEXClientSessionImpl.disconnect(OBEXClientSessionImpl.java:143)
at OfficialTest.sendMessageToDevice(OfficialTest.java:162)
at OfficialTest.servicesDiscovered(OfficialTest.java:126)
at com.intel.bluetooth.BluetoothStackMicrosoft$4.runSearchServices(BluetoothStackMicrosoft.java:524)
at com.intel.bluetooth.SearchServicesThread.run(SearchServicesThread.java:149)
java.io.EOFException: EOF while reading OBEX packet; received 0 form 3 expected
at com.intel.bluetooth.obex.OBEXUtils.readFully(OBEXUtils.java:73)
at com.intel.bluetooth.obex.OBEXUtils.readFully(OBEXUtils.java:44)
at com.intel.bluetooth.obex.OBEXSessionBase.readPacket(OBEXSessionBase.java:205)
at com.intel.bluetooth.obex.OBEXClientSessionImpl.disconnect(OBEXClientSessionImpl.java:144)
at MyDiscoveryListener.sendMessageToDevice(MyDiscoveryListener.java:168)
at MyDiscoveryListener.servicesDiscovered(MyDiscoveryListener.java:131)
at com.intel.bluetooth.BluetoothStackMicrosoft$4.runSearchServices(BluetoothStackMicrosoft.java:524)
at com.intel.bluetooth.SearchServicesThread.run(SearchServicesThread.java:149)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -bluetooth