我正在为我的MSc开发 Bluetooth 应用程序。结束项目。它包括在JAVA ME
中实现的服务器,以及用Android
编写的客户端。
问题是Android SDP
似乎无法识别我的JAVA ME服务器的ServiceRecord
。
如果我在我的客户端中使用方法BluetoothDevice.getUuids()
和BluetoothDevice.fetchUuidsWithSdp()
,则会返回一组UUIDs
,但我的服务UUID
中没有其他地方,因此我无法连接到它。
这是JAVAME
服务器示例的代码:
/*IMPORTS*/
public class StackOverFlowServer extends MIDlet {
Display mDisplay;
Form mForm;
LocalDevice local;
StreamConnectionNotifier server;
ServiceRecord sr;
String conURL;
StreamConnection conn;
public StackOverFlowServer() {
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mForm);
mForm = new Form(null);
conURL = "btspp://localhost:68EE141812D211D78EED00B0D03D76EC;"
+ "authenticate=false;encrypt=false;name=MySampleApp";
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
try {
local = LocalDevice.getLocalDevice();
} catch (BluetoothStateException e) {
// Error handling code here
}
/*
* When creating a StreamConnectionNotifier a service record
* will automatically be created for us, describing the new
* service. The service will have the Serial Port (0x1101)
* value in the ServiceClassIDList (id 0x0001) attribute. The
* service record will also have both the L2CAP (0x0100) and
* RFCOMM (0x0003) values in the ProtocolDescriptorList (id
* 0x0004) attribute. Other mandatory attributes will be set
* automatically by the JABWT implementation. The optional
* ServiceName (id 0x100) attribute will be set to the name
* parameter, "BTDemoApp" in this case.
*/
try {
server = (StreamConnectionNotifier)
Connector.open(conURL);
} catch (IOException e1) {
// Error handling code here
}
/*
* The automatically created service record can be obtained
* from the LocalDevice object, using the reference to the
* StreamConnectionNotifier.
*/
try {
sr = local.getRecord(server);
}
catch (IllegalArgumentException iae){
// Error handling code here
}
/*
* We create a new DataElement and set its content correctly.
*/
DataElement elm = null;
/*
* Setting public browse root in the browsegrouplist
* attribute. The BrowseGroupList (id 0x0005) attribute
* contains a DataElement sequence, which in turn contains
* DataElements with UUIDs. The DataElement sequence must be
* created before we can add DataElements to it.
*/
elm = new DataElement(DataElement.DATSEQ);
elm.addElement(new DataElement(
DataElement.UUID,new UUID(0x1002)));
/*
* The DataElement is now prepared. It must be added to the
* appropriate attribute ID, in this case the
* BrowseGroupList.
*/
sr.setAttributeValue(0x0005,elm);
/*
* Finally, the service record must be updated in our
* LocalDevice.
*/
try {
local.updateRecord(sr);
} catch (ServiceRegistrationException e3) {
// Error handling code here
}
try {
conn = server.acceptAndOpen();
} catch (ServiceRegistrationException sre){
// Error handling code here
} catch (IOException e2) {
// Error handling code here
}
/*
* At this point a client is connected. input and output
* streams can be obtained from the conn object,
* communication can begin.
*/
}
}
这是Android客户端的一部分:
package com.exampleproyect.stackoverflowclient;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.os.Parcelable;
public class StackOverFlowClientActivity extends Activity {
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice C702;
BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
C702 = mBluetoothAdapter.getRemoteDevice("00:23:F1:27:16:DA");
C702.fetchUuidsWithSdp();
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_UUID.equals(action)){
Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
/*uuidExtra should contain my service's UUID among his files, but it doesn't!!*/
}
}
};
// Register the BroadcastReceiver
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_UUID);
registerReceiver(mReceiver, filter1); // Don't forget to unregister during onDestroy
}
}
最让我困扰的是,我的计算机可以正确地看到我的JAVAME
服务器广告:
我做错了什么? :(为什么我的电脑的蓝牙在JAVAME
中正确地宣传我的服务,但Android不能?Android
可以通过蓝牙连接到{ {1}}服务器?
我在StackOverFlow中已经阅读了大量其他类似的问题,但我还没有找到一个可行的解决方案。 :(
我正在使用索尼爱立信C702(Java平台8)作为测试服务器。和三星Galaxy S(ICS 4.0.3,团队ICSSGS RC4.2)
EDIT1
行。
我知道问题所在。
我希望JAVAME服务器使用UUID(例如)宣传我的服务:“68EE1418-12D2-11D7-8EED-00B0D03D76EC”,但似乎代码:
JAVAME
将默认串行端口服务UUID“00001101-0000-1000-8000-00805F9B34FB”添加为ServiceClassIDList属性:
现在,有趣的是Android方法BluetoothDevice.fetchUuidsWithSdp(); 似乎取了默认的“00001101-0000-1000-8000-00805F9B34FB”UUID,而不是我的“68EE1418-12D2-11D7-8EED-00B0D03D76EC”UUID,将我的服务与手机的默认串口服务重叠。
类似中世纪蓝牙网络扫描仪,它识别我的服务,但广告为“00001101-0000-1000-8000-00805F9B34FB”:
如何删除 conURL = "btspp://localhost:68EE141812D211D78EED00B0D03D76EC;"
+ "authenticate=false;encrypt=false;name=MySampleApp";
server = (StreamConnectionNotifier)
Connector.open(conURL);
自动添加到我的服务中的额外UUID
?
请帮助,谢谢!
EDIT2
好的,问题出在索尼爱立信手机上。显然,他们会在我的自定义JAVAME
之前插入标准串口UUID {0x1101}
,而三星和诺基亚手机会在服务记录中插入此标准UUID
。 Android似乎只获取服务记录中属性UUID
的第一个值,因此,将我的服务与某些设备附带的默认串行端口混淆。
所以解决方案将是:
目前的Android API是否有办法获取除0x001
以外的更多servicerecord
个属性?
或
有没有办法在调用“acceptAndOpen”-JAVAME-后更新存储在服务发现数据库(SDDB)上的ServiceClassID (0x001)
? - 我在servicerecord
上创建了服务之后使用了LocalDevice.updateRecord
,它可以在模拟器上运行,但是它会在实际设备上执行异常并且不执行任何操作。