android wi-fi manager and bluetooth connection

17
Android WiFi and Bluetooth Jussi Pohjolainen

Upload: jussi-pohjolainen

Post on 24-May-2015

4.456 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Android Wi-Fi Manager and Bluetooth Connection

Android  Wi-­‐Fi  and  Bluetooth  

Jussi  Pohjolainen  

Page 2: Android Wi-Fi Manager and Bluetooth Connection

WI-­‐FI  MANAGER  

Page 3: Android Wi-Fi Manager and Bluetooth Connection

WifiManager  

•  WifiManager  is  able  – List  current  configured  networks  – Can  configure  the  current  ac?ve  wi-­‐fi  connec?on  – Results  of  access  point  scans,  

•  Needs  android.permission.ACCESS_WIFI_STATE  

Page 4: Android Wi-Fi Manager and Bluetooth Connection

Show  Wi-­‐Fi  Informa?on  

Page 5: Android Wi-Fi Manager and Bluetooth Connection

WifiManager’s  methods  

•  addNetwork(WifiConfigura?on  wc)  – Add  a  new  network  descrip?on  to  the  set  of  configured  networks.  

•  disableNetwork(int  netId)  – Disable  a  configured  network.    

•  List<WifiConfigura?on>  getConfiguredNetworks()  –  Return  a  list  of  all  the  networks  configured  in  the  supplicant.  

•  See:  hTp://developer.android.com/reference/android/net/wifi/WifiManager.html  

Page 6: Android Wi-Fi Manager and Bluetooth Connection

BLUETOOTH  API  

Page 7: Android Wi-Fi Manager and Bluetooth Connection

Android  Bluetooth  API  

•  Wirelessly  exchange  data  with  other  bluetooth  devices  

•  Possibili?es  – Scan  other  devices  – Connect  to  other  devices  through  discovery  – Transfer  data  – Manage  mul?ple  connec?ons    

Page 8: Android Wi-Fi Manager and Bluetooth Connection

Bluetooth  Permissions  

•  In  order  to  use  bluetooth  features,  one  must  declare  at  least  one  of  the  following:    – BLUETOOTH  •  Reques?ng,  accep?ng,  and  transferring  data  

– BLUETOOTH_ADMIN  •  Device  discovery  and  manipulate  bluetooth  se\ngs  

Page 9: Android Wi-Fi Manager and Bluetooth Connection

Se\ng  Up  Bluetooth  

Page 10: Android Wi-Fi Manager and Bluetooth Connection

Se\ng  Up  Bluetooth  

Page 11: Android Wi-Fi Manager and Bluetooth Connection

Querying  Paired  Devices  

Page 12: Android Wi-Fi Manager and Bluetooth Connection

Discovering  All  Devices  

•  To  discover  use,  startDiscovery  from  BluetoothAdapter:  – mBluetoothAdapter.startDiscovery();

•  Will  return  boolean  value  if  the  discovering  started  ok.  

•  Discovery  is  asynchronous  and  last  about  12  secs  

•  When  devices  are  discovered  broadcast  happens…  

Page 13: Android Wi-Fi Manager and Bluetooth Connection

Listening  to  Broadcasts  

•  You  must  create  a  broadcast  receiver  that  listens  when  devices  are  discovered  

•  Register  –  IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

–  registerReceiver(mReceiver, filter); –  // Don't forget to unregister during onDestroy

 

Page 14: Android Wi-Fi Manager and Bluetooth Connection

Broadcast  Receiver  // Create a BroadcastReceiver for ACTION_FOUND

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        // When discovery finds a device

        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());

        }

    }

};

Page 15: Android Wi-Fi Manager and Bluetooth Connection

Connec?ng  

•  You  must  have  a  client  and  a  server  •  Use  classes  BluetoothServerSocket  and  BluetoothSocket

•  Connec?on  is  done  in  separate  thread  •  You  have  to  have  server  and  client  

Page 16: Android Wi-Fi Manager and Bluetooth Connection

Server  

Page 17: Android Wi-Fi Manager and Bluetooth Connection

Client