基本上,我正在开发一款Android应用,只要用户点击手机上的NFC标签,就需要与Raspberry Pi建立BLE连接。该应用程序的目的是当用户通过应用程序本身或通过NFC标签访问视频时,通过Raspberry Pi在电视上播放视频。我计划在其中一个NFC标签中存储RPi的MAC地址,当点击时,应用程序会将该地址传递到应用程序中的蓝牙服务以启动连接。请注意,我必须能够使用此方法启动BLE连接。可能吗?
目前,这是我得到的
蓝牙服务类
public int onStartCommand (Intent intent, int flags, int startId){
tag = intent.getStringExtra("condition");
if(tag != null && tag.equals("start")){
//start ble connection
tag2 = intent.getStringExtra("macaddress");
String ADDRESS = tag2;
bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
device = mBluetoothAdapter.getRemoteDevice(ADDRESS);
Log.d("Capstone", device.getAddress());
bluetoothGatt = device.connectGatt(bluetooth.this, true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.d("Capstone", "BLE connected");
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
sendstatus("1");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.d("Capstone", "Services discovered");
charCapstone = gatt.getService(UUID.fromString(CAPSTONE_SERVICE_UUID))
.getCharacteristic(UUID.fromString(CAPSTONE_CHARACTERISTICS_UUID));
bluetoothGatt = gatt;
}});
}
else if(tag != null && tag.equals("started")){
tag3 = intent.getStringExtra("command");
Log.d("Capstone", tag3);
if(!tag3.equals("0")){
charCapstone.setValue(new byte[]{(byte)Integer.parseInt(tag3)});
bluetoothGatt.writeCharacteristic(charCapstone);
}
}
return START_NOT_STICKY;
}
我如何阅读NFC
private void handleIntent(Intent intent) {
Parcelable[] messages = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
for(int i = 0;i < messages.length;i++) {
NdefMessage message = (NdefMessage) messages[i];
NdefRecord[] records = message.getRecords();
NdefRecord record = records[0];
if(new String(record.getType()).equals("T")) {
byte[] original = record.getPayload();
byte[] value = Arrays.copyOfRange(original, 3, original.length);
String payload = new String(value);
if(payload.equals(ble)){
NdefRecord record2 = records[1];
if(new String(record2.getType()).equals("T")) {
byte[] original2 = record2.getPayload();
byte[] value2 = Arrays.copyOfRange(original2, 3, original2.length);
String payload2 = new String(value2);
startBluetooth(payload2);
}
}
else if(a.equals(def)){
activityfilter(payload);}
else {
bluetooth(payload);
}
}
}
}
我使用的两种蓝牙方法将NFC数据传递给蓝牙服务类。
public void startBluetooth(String c){
Intent serviceIntent = new Intent(this, bluetooth.class);
serviceIntent.putExtra("condition", "start");
serviceIntent.putExtra("macaddress", c);
this.startService(serviceIntent);
public void bluetooth(String b)
{
String s1="1";
String s2="2";
Intent serviceIntent1 = new Intent(this, bluetooth.class);
serviceIntent1.putExtra("condition", "started");
serviceIntent1.putExtra("command", b);
Log.d("Capstone", "bluetooth: " + b);
this.startService(serviceIntent1);
if(b.equals(s1)){
Intent videocontrol1 = new Intent(ModeChoose.this, videocontroller.class);
startActivity(videocontrol1);}
if(b.equals(s2)){
Intent videocontrol2 = new Intent(ModeChoose.this, videocontroller.class);
startActivity(videocontrol2);}
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
a = intent.getStringExtra("status");
String b="1";
if(a.equals(b))
{
Toast.makeText(ModeChoose.this,
"Bluetooth1 Connected", Toast.LENGTH_LONG).show();
}
}
};
我能够阅读NFC标签。但是我似乎无法与Raspberry Pi建立BLE连接。我是Android的新手。任何帮助将不胜感激。