LE ScanRecord的getServiceData()返回null

时间:2018-02-26 07:22:50

标签: android android-studio bluetooth-lowenergy

我正在尝试实施蓝牙广告客户和扫描仪。我的广告客户代码通过广告包中的一些服务数据进行广告宣传。在扫描器端,当我尝试使用在广告商中添加服务数据期间使用的相同UUID获取数据时,getService数据方法返回null对象。这是我的广告客户代码。

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

0 个答案:

没有答案
相关问题