public class MainActivity extends AppCompatActivity {
private static final int REQ_CODE_BT = 1001;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter = mBluetoothAdapter.getDefaultAdapter();
// Обработчик OnClick
View.OnClickListener OnClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bluetooth_button:
if (bluethoothButton.isChecked())
enableBluetooth();
else
disableBluetooth();
break;
}
}
};
bluethoothButton.setOnClickListener(OnClick);
}
@Override
protected void onResume() {
super.onResume();
stateBluethooth();
}
// Если блютуз не поддерживаеться
private void noSupportedBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.noSupportedBluethooth, Toast.LENGTH_LONG).show();
}
}
// Включаем блютуз
private void enableBluetooth() {
if (!mBluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQ_CODE_BT);
}
}
// Проверяем состояние блютуз
private void stateBluethooth(){
if (mBluetoothAdapter.isEnabled()) {
bluethoothButton.setChecked(true);
}
}
// Выключаем блютуз
private void disableBluetooth() {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
}
}
Добавить комментарий
|
1 ответ
Поиск устройств можно сделать таким способом:
mBluetoothAdapter.startDiscovery();
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
Получение списка сопряженных устройств происходит так:
public class PairedDeviceActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> mDeviceList = new ArrayList<>();
private void getBluetoothPairedDevices(final ArrayList<String> deviceList, final ListView listView){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "This device not support bluetooth", Toast.LENGTH_LONG).show();
} else {
if (!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
Set<BluetoothDevice> all_devices = bluetoothAdapter.getBondedDevices();
if (all_devices.size() > 0) {
for (BluetoothDevice currentDevice : all_devices) {
deviceList.add("Device Name: "+currentDevice.getName() + "\nDevice Address: " + currentDevice.getAddress());
listView.setAdapter(new ArrayAdapter<>(getApplication(),
android.R.layout.simple_list_item_1, deviceList));
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paired_device);
listView = findViewById(R.id.listView);
getBluetoothPairedDevices(mDeviceList,listView);
}
}