我正在尝试实施蓝牙广告客户和扫描仪。我的广告客户代码正在做广告,广告包中包含一些服务数据。在扫描器端,当我尝试使用在广告客户中添加服务数据期间使用的相同UUID来获取数据时,getService数据方法将返回空对象。这是我的广告客户代码。
public class Main_Service extends Service{
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeAdvertiser mBluetoothLeAdvertiser;
private Intent mIntent;
private String RSSI,SSID,MAC;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand (Intent intent, int flags, int startId) {
RSSI = intent.getStringExtra("RSSI");
SSID = intent.getStringExtra("SSID");
MAC= intent.getStringExtra("MAC");
Toast.makeText(this, RSSI+";"+SSID+";"+MAC+":", Toast.LENGTH_LONG).show();
initialize();
startadvertising();
return 0;
}
public void onCreate() {
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
}
private void startadvertising() {
AdvertiseSettings settings = buildAdvertiseSettings();
AdvertiseData data = buildAdvertiseData();
if (mBluetoothLeAdvertiser != null) {
mBluetoothLeAdvertiser.startAdvertising(settings, data, new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
Toast.makeText(getBaseContext(), "Adertising", Toast.LENGTH_LONG).show();
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
Toast.makeText(getBaseContext(), "Advertisement failed", Toast.LENGTH_LONG).show();
}
});
}
else{
Toast.makeText(getBaseContext(), "BLE is not supported", Toast.LENGTH_LONG).show();
}
}
private void initialize() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter!=null){
if(!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
}
}
else{
Toast.makeText(getBaseContext(), "BL is not supported", Toast.LENGTH_LONG).show();
}
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
}
private AdvertiseSettings buildAdvertiseSettings() {
AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
settingsBuilder.setTimeout(0);
return settingsBuilder.build();
}
private AdvertiseData buildAdvertiseData() {
AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
//String data = RSSI+SSID.substring(0,10)+MAC.replaceAll(":","");
byte[] serviceData = new byte[15];
serviceData[0] = (byte)Integer.parseInt(RSSI);
String[] macAddressParts = MAC.split(":");
for(int i=0; i<6; i++){
Integer hex = Integer.parseInt(macAddressParts[i], 16);
serviceData[i+1] = hex.byteValue();
}
for(int i=0;i<8;i++) {
serviceData[i+7] = (byte)(SSID.charAt(i));
}
long mostsignificant=0,leastsignificant=5122;
UUID s_id = new UUID(mostsignificant,leastsignificant);
dataBuilder.addServiceData(new ParcelUuid(s_id),serviceData);
Toast.makeText(this, s_id.toString(), Toast.LENGTH_LONG).show();
return dataBuilder.build();
}
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
public void onDestroy() {
mBluetoothAdapter = null;
mBluetoothLeAdvertiser = null;
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
}
最佳答案
在正常的BLE流中,我们需要在与BLE设备建立成功连接后调用DiscoverService()。我们将在onServiceDiscoverd()中获得一个回调。之后,我们需要使用相应的服务和特征UUID处理getService()。在调用discoverService()之前,getService()将始终返回null。我希望这能帮到您。如果需要,我可以分享我的完整服务流程。