我目前正在嘗試創建一個應用程序,它將使Android和IOS設備通過藍牙相互連接,然後將消息發送給另一個。下面是我的代碼爲這對Android應用程序Android到IOS藍牙聊天應用程序
MAINACTIVITY.JAVA
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothGatt gatt;
private BluetoothGattCharacteristic inputCharacteristic;
private TextView outputView;
private EditText inputView;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter.LeScanCallback leScanCallback;
public void receiveMode(View v) {
bluetoothAdapter.startLeScan(leScanCallback);
}
public void sendMessage(View v) {
inputCharacteristic.setValue(inputView.getText().toString());
gatt.writeCharacteristic(inputCharacteristic);
}
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
final String value = characteristic.getStringValue(0);
runOnUiThread(new Runnable() {
@Override
public void run() {
outputView.setText(value);
}
});
}
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
MainActivity.this.gatt = gatt;
if (newState == BluetoothProfile.STATE_CONNECTED) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService service : services) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
} else if (getString(R.string.inputUUID).equals(characteristic.getUuid().toString())) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
inputCharacteristic = characteristic;
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputView = (TextView) findViewById(R.id.outputText);
inputView = (EditText) findViewById(R.id.inputText);
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
我一直沒能把這個應用程序的工作,因爲我從Android監視器接收空回調時startLeScan運行。我相信這是因爲這種方法已被廢棄,因此不再適用。有人可以編輯我的代碼,使其使用替代方法來掃描設備?
這是錯誤消息我收到:
11月8日至2日:36:06.470 31188-31188/uk.ac.york.androidtoiosble d/BluetoothAdapter:startLeScan():空 11月8日至2日: 36:06.470 31188-31188/uk.ac.york.androidtoiosble E/BluetoothAdapter:startLeScan:null回調
回調不能爲空,那麼究竟什麼是你的錯誤信息? –
我已經添加了錯誤信息,不完全確定它的含義,我想這意味着它無法找到任何設備。 –
其實,'BluetoothAdapter.LeScanCallback leScanCallback;'永遠不會被分配,所以它是空的... –