lundi 16 février 2015

scanning bluetooth devices store in DB

when i scanning the blue tooth list show the devices which bluetooth scan , but how can i store data devices name into data base here is my code. package com.example.lifelog;



private static final int REQUEST_ENABLE_BT = 1;
private Button onBtn;
private Button findBtn;
private BluetoothAdapter myBluetoothAdapter;
private ListView myListView;
private ArrayAdapter<String> bTArrayAdapter;
public Context context;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// lifehelper= new LifeDatabaseAdapter(this);
lifehelper = new Lifehelper(this);

SQLiteDatabase sqliteDatabase =lifehelper.getWritableDatabase();


myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (myBluetoothAdapter ==null){
onBtn.setEnabled(false);
findBtn.setEnabled(false);

Toast.makeText(getApplicationContext(),"your devices does not support bluetooth",Toast.LENGTH_LONG).show();
}
else {
onBtn = (Button)findViewById(R.id.turnon);
onBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
on(v);
}
});

findBtn = (Button)findViewById(R.id.addUser);
findBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
BluetoothClass.find(v);
find(v);
}
});


} myListView = (ListView)findViewById(R.id.listView1); bTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);



myListView.setAdapter(bTArrayAdapter);
}

final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

bTArrayAdapter.add(device.getName()+"\n"+device.getAddress());
bTArrayAdapter.add(device.getName());
bTArrayAdapter.notifyDataSetChanged();
Toast.makeText(context, String.valueOf(bTArrayAdapter.getCount()), Toast.LENGTH_LONG ).show();
}
}
};
protected void on(View v) {
if(!myBluetoothAdapter.isEnabled()){
Intent turnonIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnonIntent, REQUEST_ENABLE_BT );

Toast.makeText(getApplicationContext(), "Bluetooth turned on ", Toast.LENGTH_LONG).show();

}
else {

Toast.makeText(getApplicationContext(), "bluetooth is already on ",Toast.LENGTH_LONG ).show();

}
}
protected void find(View v) { if(myBluetoothAdapter.isDiscovering()){

myBluetoothAdapter.cancelDiscovery();
}
else {

bTArrayAdapter.clear();
myBluetoothAdapter.startDiscovery();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

}
}
}

Aucun commentaire:

Enregistrer un commentaire