Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 扫描记录的getServiceData()返回null_Android_Android Studio_Bluetooth Lowenergy - Fatal编程技术网

Android 扫描记录的getServiceData()返回null

Android 扫描记录的getServiceData()返回null,android,android-studio,bluetooth-lowenergy,Android,Android Studio,Bluetooth Lowenergy,我正在尝试实现蓝牙广告商和扫描仪。我的广告商代码是广告包中的一些服务数据。在扫描端,当我尝试使用在广告客户中添加服务数据时使用的相同UUID获取数据时,getService data方法返回null对象。这是我的广告代码 public class Main_Service extends Service{ private BluetoothAdapter mBluetoothAdapter; private BluetoothLeAdvertiser mBluetoothLeAdvertiser

我正在尝试实现蓝牙广告商和扫描仪。我的广告商代码是广告包中的一些服务数据。在扫描端,当我尝试使用在广告客户中添加服务数据时使用的相同UUID获取数据时,getService data方法返回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();
}
}
公共类主\u服务扩展服务{
私人蓝牙适配器mBluetoothAdapter;
私人BluetoothLeAdvertiser mBluetoothLeAdvertiser;
私人意图;
私有字符串RSSI、SSID、MAC;
@凌驾
公共IBinder onBind(意向){
返回null;
}
公共int onStartCommand(Intent Intent、int标志、int startId){
RSSI=intent.getStringExtra(“RSSI”);
SSID=intent.getStringExtra(“SSID”);
MAC=intent.getStringExtra(“MAC”);
Toast.makeText(this,RSSI+“;”+SSID+“;”+MAC+:”,Toast.LENGTH_LONG.show();
初始化();
开始转换();
返回0;
}
public void onCreate(){
Toast.makeText(此“服务已创建”,Toast.LENGTH_LONG).show();
}
私有无效开始转换(){
AdvertiseSettings=buildAdvertiseSettings();
AdvertiseData=buildAdvertiseData();
如果(mBluetoothLeAdvertiser!=null){
mBluetoothLeAdvertiser.StartVertising(设置、数据、新广告回调(){
@凌驾
启动成功时公共无效(广告设置设置生效){
super.onStartSuccess(设置效果);
Toast.makeText(getBaseContext(),“Adertising”,Toast.LENGTH_LONG.show();
}
@凌驾
公共无效启动失败(int错误代码){
超级启动失败(错误代码);
Toast.makeText(getBaseContext(),“广告失败”,Toast.LENGTH_LONG.show();
}
});
}
否则{
Toast.makeText(getBaseContext(),“不支持BLE”,Toast.LENGTH_LONG.show();
}
}
私有void初始化(){
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter!=null){
如果(!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
}
}
否则{
Toast.makeText(getBaseContext(),“不支持BL”,Toast.LENGTH_LONG.show();
}
mBluetoothLeAdvertiser=mBluetoothAdapter.getBluetoothLeAdvertiser();
}
私有广告设置buildAdvertiseSettings(){
AdvertiseSettings.Builder settingsBuilder=新的AdvertiseSettings.Builder();
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
settingsBuilder.setTimeout(0);
返回设置builder.build();
}
私有AdvertiseData buildAdvertiseData(){
AdvertiseData.Builder dataBuilder=新的AdvertiseData.Builder();
//字符串数据=RSSI+SSID.substring(0,10)+MAC.replaceAll(“:”,”);
字节[]服务数据=新字节[15];
serviceData[0]=(字节)整数.parseInt(RSSI);
字符串[]macAddressParts=MAC.split(“:”);

对于(int i=0;i在正常的BLE流中,我们需要在与BLE设备建立成功连接后调用discoverService()。我们将在onServiceDiscoverd()中获得回调。之后,我们需要使用相应的服务和特征UUID接近getService()。在discoverService()调用之前,getService()将始终返回null。我希望这将对您有所帮助。如果您需要,我可以分享我的完整服务流程。

您可以发布扫描代码吗?可能需要检查Anroid版本,OP正在谈论的是仅通过扫描而非连接获得的版本。