Skip to content

Instantly share code, notes, and snippets.

@afjk
Last active December 18, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afjk/5828587 to your computer and use it in GitHub Desktop.
Save afjk/5828587 to your computer and use it in GitHub Desktop.
Get Bluetooth device info from BluetoothDevice class
@Override
protected void onNewIntent(Intent intent) {
// NDEF exchange mode
if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
NdefMessage[] msgs = getNdefMessages(intent);
}
// Tag writing mode
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// bluetoothDeviceからの情報取得
byte[] oobOptionalDataLength ={(byte)0x21,(byte)0x00};
//---Bluetooth Device Address
String addressStr = mBluetoothDevice.getAddress();
String[] addressBuff = addressStr.split(":");
ByteBuffer addressByteBuf = ByteBuffer.allocate(6);
for( int i = 5;i >= 0; i--){
addressByteBuf.put((byte)Integer.parseInt(addressBuff[i],16));
}
byte[] Address = addressByteBuf.array();
//---Local Name
byte[] EIR_DataType1 = {(byte)0x09};
byte[] LocalName = mBluetoothDevice.getName().getBytes();
byte[] EIR_DataLength1 = { (byte)(LocalName.length+1) };
//---Class of Device
int servicesVal = getServiesVal() & 0xFFFF0000;
servicesVal = servicesVal >> 16;
int majoreClass = mBluetoothDevice.getBluetoothClass().getMajorDeviceClass();
int minorClass = mBluetoothDevice.getBluetoothClass().getDeviceClass() & 0xFF;
majoreClass = majoreClass >> 8;
byte[] EIR_DataLength2 = {(byte)0x04};
byte[] EIR_DataType2 = {(byte)0x0D};
byte[] DeviceClass = {(byte)minorClass,(byte)majoreClass,(byte)servicesVal};
//---UUID List
byte[] EIR_DataType3 = {(byte)0x03};
ParcelUuid[] uuids = mBluetoothDevice.getUuids();
ByteBuffer uuidByteBuf = ByteBuffer.allocate(uuids.length*2);
for( int i=0; i<uuids.length;i++){
UUID uuid = uuids[i].getUuid();
String uuidStr = uuid.toString().substring(6,8);
uuidByteBuf.put((byte)Integer.parseInt(uuidStr,16));
uuidStr = uuid.toString().substring(4,6);
uuidByteBuf.put((byte)Integer.parseInt(uuidStr,16));
}
byte[] UUID_list = uuidByteBuf.array();
byte[] EIR_DataLength3 = {(byte)(UUID_list.length+1)};
int oobLen = oobOptionalDataLength.length+Address.length+EIR_DataLength1.length+EIR_DataType1.length+LocalName.length+
EIR_DataLength2.length+EIR_DataType2.length+DeviceClass.length+EIR_DataLength3.length+EIR_DataType3.length+UUID_list.length;
ByteBuffer byteBuf = ByteBuffer.allocate(oobLen);
//--OOB全体長再計算
byte[] oobLenBytes = toBytes(oobLen);
oobOptionalDataLength[0] = oobLenBytes[3];
oobOptionalDataLength[1] = oobLenBytes[2];
byteBuf.put(oobOptionalDataLength);
byteBuf.put(Address);
byteBuf.put(EIR_DataLength1);
byteBuf.put(EIR_DataType1);
byteBuf.put(LocalName);
byteBuf.put(EIR_DataLength2);
byteBuf.put(EIR_DataType2);
byteBuf.put(DeviceClass);
byteBuf.put(EIR_DataLength3);
byteBuf.put(EIR_DataType3);
byteBuf.put(UUID_list);
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "application/vnd.bluetooth.ep.oob".getBytes(),
new byte[] {}, byteBuf.array());
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] {
textRecord
});
writeTag(ndefMessage, detectedTag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment