Je développe une application qui se connecte au capteur de fréquence cardiaque et récupère les valeurs. J'utilise BLE 4.0. Avec l'aide de google developer portal, j'ai pu créer du code, mais l'application plante.Travailler avec Android Bluetooth 4.0
S'il vous plaît aidez-moi. Et aussi j'ai besoin de synchroniser les données que je n'ai pas pu utiliser correctement.
code:
public class PollingTest extends Activity{
public Button btn2;
public static String UUID = "00001101-0000-1000-8000-00805F9B34FB";
public boolean mScanning;
public Handler mHandler;
public LeDeviceListAdapter mLeDeviceListAdapter;
public BluetoothAdapter mBluetoothAdapter;
static final int REQUEST_ENABLE_BT = 1;
public static final long SCAN_PERIOD = 10000; // Stops scanning after 10 seconds.
@Override
protected void onCreate(Bundle savedInstanceStat){
super.onCreate(savedInstanceStat);
setContentView(R.layout.pollingactivity);
btn2 = (Button)findViewById(R.id.pollB);
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHandler = new Handler();
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(getApplicationContext(), "Bluetooth Low Energy is not supported on this Device ", Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth not Supported", Toast.LENGTH_SHORT).show();
finish();
return;
}
else {
if (!mBluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
} else {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
scanLeDevice(true);
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
//mLeDeviceListAdapter.clear();
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//String macAddress;
//macAddress = device.getAddress();
Toast.makeText(getApplicationContext(),device.getAddress(),Toast.LENGTH_LONG).show();
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
/*switch (device.getAddress().toUpperCase()){
case "8C:DE:52:64:8E:D1":
Toast.makeText(getApplicationContext(), "First",Toast.LENGTH_LONG).show();
}*/
}
});
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<>();
mInflator = PollingTest.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
}
Ici, je l'ai commenté onResume & OnPause méthodes parce que, si ne faites pas, l'application est de s'écrasé.
Je pense appareil dans Lescan si pas en mesure d'accéder à/objet de réception Bluetooth pour une raison bizarre ou quelque chose. S'il vous plaît, aidez-moi
Merci Vous
J'ai réfréné à PollingTest et pourtant je reçois la même erreur –
Donnez-moi le code exact de comment vous avez changé 'DeviceScanActivity.this.getLayoutInflater();' – geokavel
Sorry man. Comme je vous l'ai dit, j'ai fait du code avec l'aide du portail Google Dev. Dans ce portail, la classe s'appelait 'DeviceScanActivity'. J'aurais dû changer 'DeviceScanActivity.this.getLayoutInflater();' à mon nom de classe .i.e., 'PollingTest.this.getLayoutInflater();' ce que je n'ai pas fait. [Voici le lien] (https://android.googlesource.com/platform/development/+/7167a054a8027f75025c865322fa84791a9b3bd1/samples/BluetoothLeGatt/src/com/example/bluetooth/le/DeviceScanActivity.java) à partir de laquelle j'ai fait, Où ils essayer de récupérer des périphériques et afficher sur listView, mais je veux récupérer et transférer des données –