List of usage examples for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE
String ACTION_REQUEST_ENABLE
To view the source code for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE.
Click Source Link
From source file:com.xiaocheng.xc_application.bluetoothutils.DeviceScanActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_bluetooth_list); //getActionBar().setTitle(R.string.title_devices); mHandler = new Handler(); //???// w w w . jav a 2 s . c om if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //?? ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, /*MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION*/0); //?? ???? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) { Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show(); } } // 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(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { /* Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); return;*/ Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } lv_bleList = (ListView) findViewById(R.id.lv_bleList); mDevListAdapter = new DeviceListAdapter(); lv_bleList.setAdapter(mDevListAdapter); lv_bleList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mDevListAdapter.getCount() > 0) { BluetoothDevice device1 = mDevListAdapter.getItem(position); if (device1 == null) return; Intent intent1 = new Intent(); intent1.putExtra(EXTRAS_DEVICE_NAME, device1.getName()); intent1.putExtra(EXTRAS_DEVICE_ADDRESS, device1.getAddress()); if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } // setResult intent data Activity setResult(Activity.RESULT_OK, intent1); //? Activity finish(); // startActivity(intent1); } } }); }
From source file:com.mooshim.mooshimeter.main.ScanActivity.java
@Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); super.onCreate(savedInstanceState); // 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(this, R.string.ble_not_supported, Toast.LENGTH_LONG).show(); }/*from w ww. j a v a2 s. co m*/ // Checks if Bluetooth is supported on the device. if (BluetoothAdapter.getDefaultAdapter() == null) { Toast.makeText(this, R.string.bt_not_supported, Toast.LENGTH_LONG).show(); setError("BLE not supported on this device"); } if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) { // Request BT adapter to be turned on Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQ_ENABLE_BT); } setContentView(R.layout.fragment_scan); // Initialize widgets mStatus = (TextView) findViewById(R.id.status); mBtnScan = (Button) findViewById(R.id.btn_scan); mDeviceScrollView = (LinearLayout) findViewById(R.id.device_list); mEmptyMsg = (TextView) findViewById(R.id.no_device); mDeviceScrollView.setClickable(true); mInflater = LayoutInflater.from(this); }
From source file:com.example.mego.adas.bluetooth.ConnectFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_connect, container, false); initializeScreen(rootView);/*from w w w. j ava 2 s. c o m*/ connectFragment = (ConnectFragment) getFragmentManager().findFragmentById(R.id.fragment_container); //check if the device has a bluetooth or not //and show Toast message if it does't have mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false); devicesRecycler.setLayoutManager(layoutManager); devicesRecycler.setHasFixedSize(true); bluetoothDevicesAdapter = new BluetoothDevicesAdapter(this); devicesRecycler.setAdapter(bluetoothDevicesAdapter); if (mBluetoothAdapter == null) { if (toast != null) { toast.cancel(); } if (connectFragment.isAdded()) { toast = Toast.makeText(getContext(), R.string.does_not_have_bluetooth, Toast.LENGTH_LONG); toast.show(); emptyTextView.setVisibility(View.VISIBLE); } } else if (!mBluetoothAdapter.isEnabled()) { Intent enableIntentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntentBluetooth, REQUEST_ENABLE_BT); emptyTextView.setVisibility(View.VISIBLE); } else if (mBluetoothAdapter.isEnabled()) { PairedDevicesList(); } if ((mBluetoothAdapter != null)) { setBroadCastReceiver(); } //request location permission for bluetooth scanning for android API 23 and above ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_ENABLE_FINE_LOCATION); //press the button to start search new Devices searchForNewDevices.setOnClickListener(v -> { if (mBluetoothAdapter != null) { searchForNewDevices.setEnabled(false); bluetoothDevicesAdapter.clear(); PairedDevicesList(); NewDevicesList(); } else { makeText(getContext(), R.string.does_not_have_bluetooth, Toast.LENGTH_LONG).show(); } }); // Inflate the layout for this fragment return rootView; }
From source file:com.techjoynt.android.nxt.fragment.NXTFragment.java
@Override public void onStart() { super.onStart(); Bundle args = getArguments();/* w w w . j av a2 s . c o m*/ if (args != null) { connectToNXT(args.getString("device_address")); } else { if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); } else { if (mSavedState == NXTTalker.STATE_CONNECTED) { BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mDeviceAddress); mNXTTalker.connect(device); } else { if (mNewLaunch) { mNewLaunch = false; findBrick(); } } } } }
From source file:com.nordicsemi.UART_UDP_PROXY.MainActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//from w w w. j av a 2s.c o m this.m_this = this; mBtAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBtAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); finish(); return; } messageListView = (ListView) findViewById(R.id.listMessage); listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail); messageListView.setAdapter(listAdapter); messageListView.setDivider(null); btnConnectDisconnect = (Button) findViewById(R.id.btn_select); exitButton = (Button) findViewById(R.id.exitButton); exitButton.setEnabled(true); btnConnectDisconnect.setEnabled(true); mRemoteRssiVal = (TextView) findViewById(R.id.rssival); mRemoteRssiVal.setText(""); service_init(); listAdapter.clear(); listAdapter.add("Ready..."); messageListView.smoothScrollToPosition(0); listAdapter.notifyDataSetChanged(); messageListView.invalidateViews(); // disable the screen saver getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON); // Handler Disconnect & Connect button btnConnectDisconnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!mBtAdapter.isEnabled()) { Log.i(TAG, "onClick - BT not enabled yet"); Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); } else { if (btnConnectDisconnect.getText().equals("Connect")) { //Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class); startActivityForResult(newIntent, REQUEST_SELECT_DEVICE); } else { //Disconnect button pressed if (mDevice != null) { // disconnect mService.disconnect(); // clear the UI listAdapter.clear(); listAdapter.add("Ready..."); messageListView.smoothScrollToPosition(0); listAdapter.notifyDataSetChanged(); messageListView.invalidateViews(); } } } } }); // Handler Send button exitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // DA mService.disconnect(); Log.d(TAG, "exiting proxy application..."); finish(); System.exit(0); } }); // Set initial UI state }
From source file:mobi.monaca.framework.plugin.BluetoothPlugin.java
/** * Execute a bluetooth function/* w w w . j ava 2s. c o m*/ */ @Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult pluginResult = null; Log.d("BluetoothPlugin", "Action: " + action); if (ACTION_ENABLE.equals(action)) { // Check if bluetooth isn't disabled already if (!m_bluetoothAdapter.isEnabled()) { m_stateChanging = true; ctx.startActivityForResult(this, new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1); while (m_stateChanging) { } ; } // Check if bluetooth is enabled now if (m_bluetoothAdapter.isEnabled()) { pluginResult = new PluginResult(PluginResult.Status.OK); } else { pluginResult = new PluginResult(PluginResult.Status.ERROR); } } // Want to disable bluetooth? else if (ACTION_DISABLE.equals(action)) { if (!m_bluetoothAdapter.disable() && m_bluetoothAdapter.isEnabled()) { pluginResult = new PluginResult(PluginResult.Status.ERROR); } else { pluginResult = new PluginResult(PluginResult.Status.OK); } } else if (ACTION_DISCOVERDEVICES.equals(action)) { m_discoveredDevices = new JSONArray(); if (!m_bluetoothAdapter.startDiscovery()) { pluginResult = new PluginResult(PluginResult.Status.ERROR, "Unable to start discovery"); } else { m_discovering = true; // Wait for discovery to finish while (m_discovering) { } Log.d("BluetoothPlugin", "DiscoveredDevices: " + m_discoveredDevices.length()); pluginResult = new PluginResult(PluginResult.Status.OK, m_discoveredDevices); } } // Want to list UUIDs of a certain device else if (ACTION_GETUUIDS.equals(action)) { try { String address = args.getString(0); Log.d("BluetoothPlugin", "Listing UUIDs for: " + address); // Fetch UUIDs from bluetooth device BluetoothDevice bluetoothDevice = m_bluetoothAdapter.getRemoteDevice(address); Method m = bluetoothDevice.getClass().getMethod("fetchUuidsWithSdp"); Log.d("BluetoothPlugin", "Method: " + m); m.invoke(bluetoothDevice); m_gettingUuids = true; while (m_gettingUuids) { } pluginResult = new PluginResult(PluginResult.Status.OK, m_gotUUIDs); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } // Connect to a given device & uuid endpoint else if (ACTION_CONNECT.equals(action)) { try { String address = args.getString(0); UUID uuid = UUID.fromString(args.getString(1)); Log.d("BluetoothPlugin", "Connecting..."); BluetoothDevice bluetoothDevice = m_bluetoothAdapter.getRemoteDevice(address); BluetoothSocket bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid); bluetoothSocket.connect(); m_bluetoothSockets.add(bluetoothSocket); int socketId = m_bluetoothSockets.indexOf(bluetoothSocket); pluginResult = new PluginResult(PluginResult.Status.OK, socketId); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } else if (ACTION_READ.equals(action)) { try { int socketId = args.getInt(0); BluetoothSocket bluetoothSocket = m_bluetoothSockets.get(socketId); InputStream inputStream = bluetoothSocket.getInputStream(); char[] buffer = new char[128]; for (int i = 0; i < buffer.length; i++) { buffer[i] = (char) inputStream.read(); } //Log.d( "BluetoothPlugin", "Buffer: " + String.valueOf(buffer) ); pluginResult = new PluginResult(PluginResult.Status.OK, String.valueOf(buffer)); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } else if (ACTION_DISCONNECT.equals(action)) { try { int socketId = args.getInt(0); // Fetch socket & close it BluetoothSocket bluetoothSocket = m_bluetoothSockets.get(socketId); bluetoothSocket.close(); // Remove socket from internal list m_bluetoothSockets.remove(socketId); // Everything went fine... pluginResult = new PluginResult(PluginResult.Status.OK); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } return pluginResult; }
From source file:com.pipirssolutions.aeseztargetcontroller.ProgramFragment.java
@Override public void onStart() { super.onStart(); // If BT is not on, request that it be enabled. // setupChat() will then be called during onActivityResult if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); // Otherwise, setup the chat session } else if (mCommunicationService == null) { setupTransmission();/*from ww w . j a v a 2 s . c om*/ } }
From source file:org.apache.cordova.sipkita.BluetoothPrinter.java
void listBT(CallbackContext callbackContext) { BluetoothAdapter mBluetoothAdapter = null; String errMsg = null;// ww w . ja v a 2s.c o m try { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothPort = BluetoothPort.getInstance(); if (mBluetoothAdapter == null) { errMsg = "No bluetooth adapter available"; Log.e(LOG_TAG, errMsg); callbackContext.error(errMsg); return; } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); this.cordova.getActivity().startActivityForResult(enableBluetooth, 2); } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { JSONArray json = new JSONArray(); for (BluetoothDevice device : pairedDevices) { json.put(device.getName()); } callbackContext.success(json); } else { callbackContext.error("No Bluetooth Device Found"); } // Log.d(LOG_TAG, "Bluetooth Device Found: " + mmDevice.getName()); } catch (Exception e) { errMsg = e.getMessage(); Log.e(LOG_TAG, errMsg); e.printStackTrace(); callbackContext.error(errMsg); } }
From source file:com.powehnko.ifroglabbt_ap_pins.MainActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//from w ww.j a va 2 s. c o m mBtAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBtAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); // finish(); return; } messageListView = (ListView) findViewById(R.id.listMessage); listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail); messageListView.setAdapter(listAdapter); messageListView.setDivider(null); btnConnectDisconnect = (Button) findViewById(R.id.btn_select); // btnSend=(Button) findViewById(R.id.sendButton); // edtMessage = (EditText) findViewById(R.id.sendText); service_init(); mifrog = new ifrog(); mifrog.setTheListener(this); // Handler Disconnect & Connect button btnConnectDisconnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!mBtAdapter.isEnabled()) { Log.i(TAG, "onClick - BT not enabled yet"); Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); } else { if (btnConnectDisconnect.getText().equals("Connect")) { //Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class); startActivityForResult(newIntent, REQUEST_SELECT_DEVICE); } else { //Disconnect button pressed if (mDevice != null) { mService.disconnect(); } } } } }); ////////////// button2on = (Button) findViewById(R.id.button2on); button2on.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FunSendString("m2 on"); } }); button2off = (Button) findViewById(R.id.button2off); button2off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FunSendString("m2 off"); } }); button3on = FunSetButton(R.id.button3on, "m3 on"); button3off = FunSetButton(R.id.button3off, "m3 off"); button4on = FunSetButton(R.id.button4on, "m4 on"); button4off = FunSetButton(R.id.button4off, "m4 off"); button5on = FunSetButton(R.id.button5on, "m5 on"); button5off = FunSetButton(R.id.button5off, "m5 off"); button6on = FunSetButton(R.id.button6on, "m6 on"); button6off = FunSetButton(R.id.button6off, "m6 off"); button7on = FunSetButton(R.id.button7on, "m7 on"); button7off = FunSetButton(R.id.button7off, "m7 off"); button8on = FunSetButton(R.id.button8on, "m8 on"); button8off = FunSetButton(R.id.button8off, "m8 off"); button9on = FunSetButton(R.id.button9on, "m9 on"); button9off = FunSetButton(R.id.button9off, "m9 off"); }
From source file:fr.bmartel.android.flowerpower.FlowerPowerActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flower_power); // 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(this, "Bluetooth Smart is not supported on your device", Toast.LENGTH_SHORT).show(); finish();/*from w w w . ja v a 2 s.c o m*/ } mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } final ProgressBar progress_bar = (ProgressBar) findViewById(R.id.scanningProgress); if (progress_bar != null) progress_bar.setEnabled(false); final Button button_stop_scanning = (Button) findViewById(R.id.stop_scanning_button); if (button_stop_scanning != null) button_stop_scanning.setEnabled(false); final TextView scanText = (TextView) findViewById(R.id.scanText); if (scanText != null) scanText.setText(""); registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); button_stop_scanning.setEnabled(false); final Button button_find_accessory = (Button) findViewById(R.id.scanning_button); button_stop_scanning.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (currentService.isScanning()) { currentService.stopScan(); if (progress_bar != null) { progress_bar.setEnabled(false); progress_bar.setVisibility(View.GONE); } if (scanText != null) scanText.setText(""); if (button_stop_scanning != null) button_stop_scanning.setEnabled(false); } } }); button_find_accessory.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { triggerNewScan(); } }); if (mBluetoothAdapter.isEnabled()) { Intent intent = new Intent(this, FlowerPowerBtService.class); // bind the service to current activity and create it if it didnt exist before startService(intent); bound = bindService(intent, mServiceConnection, BIND_AUTO_CREATE); } }