2017-10-30 17 views
0

Ble이 광고입니다. BluetoothGattServer에 AdvertiseData가 추가되어 광고가 완료되고 장치가 연결됩니다. connect after discoverServices()도 호출되지만 서비스가 발견되지 않습니다. 즉, onServiceDiscovered() 콜백이 트리거되지 않습니다. 내가 을 테스트 한 구글 샘플 Adevertiser하지만 서버 측에Android BLE 서비스 onServiceDiscover()가 호출되지 않습니다.

// BluetoothGatt 클라이언트 잘 작동 나는 BLE 광고가 나열

// 광고주 코드 /** * 아래 BluetoothGattServer 코드를 시작합니다 추가했습니다. */

private void startAdvertising() { 
    Log.d(TAG, "Service: Starting Advertising"); 

    if (mAdvertiseCallback == null) { 
     AdvertiseSettings settings = buildAdvertiseSettings(); 
     AdvertiseData data = buildAdvertiseData(); 
     mAdvertiseCallback = new SampleAdvertiseCallback(); 

     if (mBluetoothLeAdvertiser != null) { 
      mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback); 
     } 
    } 
} 

// 처리하기 위해 모든 GATT 클라이언트 연결을 처리하기 위해

private void initServer() { 
    BluetoothGattService service = 
      new BluetoothGattService(Constants.Service_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY); 

    BluetoothGattCharacteristic characteristic = 
      new BluetoothGattCharacteristic(Constants.Char_UUID, 
        //Read + write characteristics and permission 
        BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE, 
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE); 

    service.addCharacteristic(characteristic); 
    mBluetoothGattServer.addService(service); 
} 

// 콜백을 노출하려고하는 서비스와 특성을 부착 GATT 서버 인스턴스를 생성 GATT 서버에 요청

읽기 + 쓰기
private BluetoothGattServerCallback mBGattServerCallback = new BluetoothGattServerCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { 
     super.onConnectionStateChange(device, status, newState); 
     Log.d(TAG ,"OnConnection state change"); 
     if(newState == BluetoothProfile.STATE_CONNECTED){ 

     }else if(newState == BluetoothProfile.STATE_DISCONNECTED){ 

     } 

    } 

    @Override 
    public void onServiceAdded(int status, BluetoothGattService service) { 
     super.onServiceAdded(status, service); 
     Log.i(TAG, " Service are added"); 
    } 

// 광고 데이터

,366,595,335 763,210

AdvertiseData data = new AdvertiseData.Builder() .setIncludeDeviceName(true) .addServiceUuid(new ParcelUuid(Constants.Service_UUID)) .build(); }

// 우리는 이미이없는 경우

private AdvertiseSettings buildAdvertiseSettings() { 
    AdvertiseSettings settings = new AdvertiseSettings.Builder() 
      .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED) 
      .setConnectable(true) 
      .setTimeout(0) 
      .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM) 
      .build(); 
    return settings; 

}

// 블루투스가 시스템 개체에 대한 참조를 가져옵니다 설정 광고.

private void initialize() { 
    if (mBluetoothLeAdvertiser == null) { 
     BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
     if (mBluetoothManager != null) { 
      BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter(); 
      if (mBluetoothAdapter != null) { 
       mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); 
       //register the instance of server to BLE framework 
       mBluetoothGattServer = mBluetoothManager.openGattServer(this,mBGattServerCallback); 
       initServer(); 
      } else { 
       Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); 
      } 
     } else { 
      Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

// 로그

` // 서비스 UUID를 여름과 특성이 광고에 데이터로 { onGetService()오고 UUID - 기기 = 65 : 5E : 34 : E1 : 47 : 55 UGID = 0000xxxx-0000-1000-8000-00805xxxx 10-31 17 : 55 : 11.542 23276-25749/ D/BluetoothGatt : onGetService() - 장치 = 65 : 5E : 34 : E1 : 47 : 55 UUID = 0000xxxx- 0000- 1000-8000-0080xxxxx

onGetCharacteristic() - Device = 65 : 5 E : 34 : E1 : 47 : 55 UUID = 0000xxxxx-0000-1000-8000-00805fxxxxx srvcType = 0 srvcInstId = 0 charInstId = 0 charProps = 32 10-31 17 : 55 : 11.627 23276-23290/ D/BluetoothGatt : onGetCharacteristic() - 장치 = 65 : 5E : 34 : E1 : 47 : 55 UUID = 0000xxx-0000-1000-8000-00805fxxxx srvcType = 0 srvcInstId = 0 charInstId = 1 charProps = 10 10-31 17 : 55 : 11.718 23276 -23289/ D/BluetoothGatt : onGetCharacteristic() - 장치 = 65 : 5E : 34 : E1 : 47 : 55 UUID = 0000xxxx-0000-1000-8000-00805fxxxxx srvcType = 0 srvcInstId = 0 charInstId = 0 charProps = 2 } `

+1

코드를 공유하십시오 –

+1

코드를 표시하십시오 –

+0

코드를 공유하십시오 plz. –

답변

0

기기 변경 저는 micromax 캔버스를 중앙으로 테스트했습니다. 이제 장치가 변경되었으며 여기 서비스가 발견되어 콜백을 받았습니다.

BUT writeCharacteristics() 요청 서버 (원격 장치)가 실행되지 않고 콜백 onCharacteristicsReadRequest() 내가 수행되었을 때만 필요 특성에 대한 기록 동작. 작동을 거부 할 다른 것.