Back to project page androidcomplete.
The source code is released under:
MIT License
If you think the Android project androidcomplete listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.core.connmanager; // w w w.ja v a2 s . c om import java.util.Set; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.widget.ArrayAdapter; import android.app.Activity; /** * Use of Android Bluetooth API for basic features * Using the Bluetooth APIs, an Android application can perform the following: * Scan for other Bluetooth devices * Query the local Bluetooth adapter for paired Bluetooth devices * Establish RFCOMM channels * Connect to other devices through service discovery * Transfer data to and from other devices * Manage multiple connections * @author onurdundar */ public class BTManager { static String TAG = "BlueTManager"; static BluetoothManager mBluetoothManager; static BluetoothDevice mBluetoothDevice; static BluetoothAdapter mBluetoothAdapter; static BluetoothSocket mBluetoothSocket; static ArrayAdapter<BluetoothDevice> discoveredDevices; private final static int REQUEST_ENABLE_BT = 1; /** * Get BT Adapter and enable if it is disabled */ public BTManager(Activity activity){ /** * Request BT Adapter from device */ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null){ Log.e(TAG, "Device doesn't support BlueTooth"); }else{ Log.i(TAG, "Device support BlueTooth"); } /** * Enable BT if it is not enabled */ if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } /** * Scan for available Bluetooth Devices and return the list of devices * @return List of devices */ public static ArrayAdapter<BluetoothDevice> scanBluetoothDevices(Activity activity){ mBluetoothAdapter.startDiscovery(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); activity.registerReceiver(mReceiver, filter); return discoveredDevices; } /** * Cancel Discovery and unregister broadcast receiver */ public static void cancelBTDiscovery(Activity activity){ mBluetoothAdapter.cancelDiscovery(); activity.unregisterReceiver(mReceiver); } /** * Query Paired Devices */ public static Set<BluetoothDevice> getPairedDevices(){ return mBluetoothAdapter.getBondedDevices(); } /** * Enable - Disable Discoverability */ private final static 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 discoveredDevices.add(device); } } }; }