use*_*496 5 service android bluetooth java-me
我正在Bluetooth为我的MSc 开发一个应用程序.结束项目.它包括一个实现的服务器JAVA ME和一个写入的客户端Android.
问题是Android SDP似乎无法识别ServiceRecord我的JAVA ME服务器.
如果我使用这些方法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.
*/
}
}
Run Code Online (Sandbox Code Playgroud)
这是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
}
}
Run Code Online (Sandbox Code Playgroud)
最困扰我的是,我的电脑可以JAVAME正确地看到我的服务器广告:

我究竟做错了什么??:(为什么我的电脑Bluetooth看我的服务在正确传播JAVAME而Android不能??可以Android通过连接Bluetooth到JAVAME服务器?
我在StackOverFlow中已经阅读了大量其他类似的问题,但我还没有找到一个可行的解决方案.:(
我正在使用索尼爱立信C702(Java平台8)作为测试服务器.和三星Galaxy S(ICS 4.0.3,团队ICSSGS RC4.2)
EDIT1
好.
我知道问题是什么.
我希望JAVAME服务器用UUID(例如)宣传我的服务:"68EE1418-12D2-11D7-8EED-00B0D03D76EC",但似乎代码:
conURL = "btspp://localhost:68EE141812D211D78EED00B0D03D76EC;"
+ "authenticate=false;encrypt=false;name=MySampleApp";
server = (StreamConnectionNotifier)
Connector.open(conURL);
Run Code Online (Sandbox Code Playgroud)
添加默认串行端口服务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":

我能做什么,除去额外的UUID那个JAVAME自动添加到我的服务?
请帮忙,谢谢!
EDIT2
好的,问题出在索尼爱立信手机上.显然他们UUID {0x1101}在我的自定义之前插入标准串口UUID,而三星和诺基亚手机,例如,UUID在我的服务记录上插入这个标准.Android似乎只获取0x001服务记录中属性的第一个值,因此,将我的服务与某些设备附带的默认串行端口混淆.
所以解决方案将是:
有没有办法使用当前的Android API来获取更多servicerecord属性ServiceClassID (0x001)?
要么
有没有办法servicerecord在调用"acceptAndOpen"-JAVAME-后更新存储在服务发现数据库(SDDB)上的内容?- 我已经LocalDevice.updateRecord在服务上创建使用了SDDB它,它可以在模拟器上运行,但是在实际设备上提供异常并且什么都不做.
| 归档时间: |
|
| 查看次数: |
2687 次 |
| 最近记录: |