0 évaluation0% ont trouvé ce document utile (0 vote)
35 vues2 pages
This document discusses Bluetooth and WiFi connectivity on Android. It provides code snippets for:
1) Scanning for Bluetooth devices, connecting, and transferring data using the BluetoothAdapter API.
2) Listing paired Bluetooth devices and checking the Bluetooth state.
3) Managing WiFi connectivity using the WifiManager to enable/disable WiFi and scan for access points.
This document discusses Bluetooth and WiFi connectivity on Android. It provides code snippets for:
1) Scanning for Bluetooth devices, connecting, and transferring data using the BluetoothAdapter API.
2) Listing paired Bluetooth devices and checking the Bluetooth state.
3) Managing WiFi connectivity using the WifiManager to enable/disable WiFi and scan for access points.
This document discusses Bluetooth and WiFi connectivity on Android. It provides code snippets for:
1) Scanning for Bluetooth devices, connecting, and transferring data using the BluetoothAdapter API.
2) Listing paired Bluetooth devices and checking the Bluetooth state.
3) Managing WiFi connectivity using the WifiManager to enable/disable WiFi and scan for access points.
//tasks: scan bluetooth devices, connect and transfer data, manage multiple connection //bluetooth api //BluetoothAdapter: initiate device discovery, query a list of parid devices, create BluetoothServerSocket to listen for connection
//enable/disable and discovery
//#1 activity layout //#2 permission in manifest <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> //#3 get bluetooth and views inastance in onCreate final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //#4 check if device support in onCreate if(adapter == null){outTextView.append("device not supported");} //#5 bluetooth enable via intent in button click if(!adapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 0); } //#6 bluetooth discovery via intent in button click if(!adapter.isDiscovering()){Toast.makeText(getApplicationContext(), "make your device discoverable");} Intent intent - new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(intent, 0); //#7 bluetooth disable adapter.disable(); Toast(getApplicationContext(), "turning off bluetooth"); /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////// //Bluetooth list paired
//#1 activity layout
//#2 permission in manifest //#3 get bluetooth instance in onCreate() final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); textview1.append("\nAdapter " + adapter); checkBluetoothState(); //#4 if enable bluetooth, check bluetooth state and pairing device again @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(resultCode, resultCode, data); if(resultCode == 1){ checkBluetoothState(); } } //#5 destroy activity @Override protected void onDestroy(){ super.onDestroy(); }
//#6 check bluetooth state(encapsulate)
private void checkBluetoothState(){ if(adapter == null){ textview1.append("\nBluetooth not supported"); return;} else{ if(adapter.isEnabled()){ textview1.append("\nBluetooth enabled"); } //#7 listing paired devices textview1.append("\nPaired Devices are: "); Set<BluetoothDevice> devices = adapter.getBondedDevices(); for(BluetoothDevice device : devices){ textview1.append("\n Devices: " + device.getName() + ", " + device); } //#8 prompet user to turn bluetooth }else{ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } } /////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////// //wifi //WifiManager: manage conectivity, add and disable network, sacan for access point, disconnect
//disable and enable wifi
//#1 activity layout //#2 get wifi instance and enable in onCreate WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true); //#3 disable wifi in button click wifi.setWifiEnabled(false); //#4 permission in manifest: ACCESS_WIFI_STATE, INTERNET, CHANGE_WIFI_STATE