public class Connect_Bluetooth extends Activity {
private ProgressDialog mProgressDlg;
private static final int REQUEST_ENABLE_BT = 1;
private TextView onBtn;
private TextView offBtn;
private TextView listBtn;
private TextView findBtn;
private TextView text;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
/* private ArrayAdapter<String> BTArrayAdapter;*/
private DeviceListAdapter mAdapter;
private ArrayList<BluetoothDevice> mDeviceList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_band);
text = (TextView) findViewById(R.id.text);
onBtn = (TextView)findViewById(R.id.turnOn);
offBtn = (TextView)findViewById(R.id.turnOff);
listBtn = (TextView)findViewById(R.id.paired);
findBtn = (TextView)findViewById(R.id.search);
myListView = (ListView)findViewById(R.id.listView1);
mDeviceList = new ArrayList<BluetoothDevice>();
mAdapter = new DeviceListAdapter(this);
mProgressDlg = new ProgressDialog(this);
mProgressDlg.setMessage("Scanning...");
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mProgressDlg.setCancelable(false);
mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
myBluetoothAdapter.cancelDiscovery();
}
});
if (myBluetoothAdapter == null) {
showUnsupported();
} else {
listBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();
if (pairedDevices == null || pairedDevices.size() == 0) {
showToast("No Paired Devices Found");
} else {
if (pairedDevices == null || pairedDevices.size() == 0) {
showToast("No Paired Devices Found");
} else {
ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();
list.addAll(pairedDevices);
mAdapter.setData(mDeviceList);
myListView.setAdapter(mAdapter);
}
}
}
});
findBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (myBluetoothAdapter.isDiscovering()) {
// the button is pressed when it discovers, so cancel the discovery
myBluetoothAdapter.cancelDiscovery();
}
else {
// BTArrayAdapter.clear();
myBluetoothAdapter.startDiscovery();
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
}
});
onBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
}
});
offBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myBluetoothAdapter.disable();
showDisabled();
}
});
if (myBluetoothAdapter.isEnabled()) {
showEnabled();
} else {
showDisabled();
}
}
mAdapter.setListener(new DeviceListAdapter.OnPairButtonClickListener() {
@Override
public void onPairButtonClick(int position) {
BluetoothDevice device = mDeviceList.get(position);
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
unpairDevice(device);
} else {
showToast("Pairing...");
pairDevice(device);
}
}
});
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void unpairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("removeBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onPause() {
if (myBluetoothAdapter != null) {
if (myBluetoothAdapter.isDiscovering()) {
myBluetoothAdapter.cancelDiscovery();
}
}
super.onPause();
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
private void showEnabled() {
text.setText("Bluetooth is On");
text.setTextColor(Color.BLUE);
offBtn.setEnabled(true);
onBtn.setEnabled(false);
}
private void showDisabled() {
text.setText("Bluetooth is Off");
text.setTextColor(Color.RED);
offBtn.setEnabled(false);
onBtn.setEnabled(true);
}
private void showUnsupported() {
text.setText("Bluetooth is unsupported by this device");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
if(myBluetoothAdapter.isEnabled()) {
showEnabled();
} else {
text.setText("Status: Disabled");
}
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
if (state == BluetoothAdapter.STATE_ON) {
showToast("Enabled");
showEnabled();
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
mDeviceList = new ArrayList<BluetoothDevice>();
mProgressDlg.show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mProgressDlg.dismiss();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device);
showToast("Found device " + device.getName());
mAdapter.setData(mDeviceList);
myListView.setAdapter(mAdapter);
}
}
};
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
Hello. You can upload image of app? (file activity xml). Tks
ReplyDelete