简体   繁体   English

LE ScanRecord 的 getServiceData() 返回 null

[英]getServiceData() of LE ScanRecord returning null

I'm trying to implement Bluetooth advertiser and scanner.我正在尝试实施蓝牙广告商和扫描仪。 My advertiser code is advertising with some service data in the advertisement packet.我的广告客户代码正在用广告数据包中的一些服务数据进行广告。 On the scanner side when I tried to get the data using the same UUID that is used during adding service data in the advertiser the getService data method is returning null object.在扫描仪方面,当我尝试使用在广告商中添加服务数据期间使用的相同 UUID 获取数据时,getService 数据方法返回空对象。 Here is my advertiser code.这是我的广告客户代码。

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();
}
}

In normal BLE flow, We need to call discoverService() after establishing a successful connection with the BLE device.在正常的BLE流程中,我们需要在与BLE设备建立成功连接后调用discoverService()。 We will get a callback in onServiceDiscoverd().我们将在 onServiceDiscoverd() 中得到一个回调。 After that, we need to approach the getService() with corresponding Service & Characteristic UUID.之后,我们需要使用相应的 Service & Characteristic UUID 来处理 getService()。 Before discoverService() call, the getService() will always return null.在discoverService() 调用之前,getService() 将始终返回null。 I hope this will help you.我希望这能帮到您。 If you required, I can share my complete service flow.如果您需要,我可以分享我的完整服务流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM