Example usage for android.bluetooth BluetoothDevice ACTION_FOUND

List of usage examples for android.bluetooth BluetoothDevice ACTION_FOUND

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice ACTION_FOUND.

Prototype

String ACTION_FOUND

To view the source code for android.bluetooth BluetoothDevice ACTION_FOUND.

Click Source Link

Document

Broadcast Action: Remote device discovered.

Usage

From source file:org.envirocar.app.application.service.DeviceInRangeService.java

@Override
public void onCreate() {
    logger.info("onCreate " + getClass().getName() + "; Hash: " + System.identityHashCode(this));
    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

    discoveryHandler = new Handler();

    bindToBackgroundService();/*from   w ww .  j  a  va  2s .c  om*/
}

From source file:com.spondbob.bluetooth.BluetoothActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup the window
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);

    // Set result CANCELED in case the user backs out
    setResult(Activity.RESULT_CANCELED);

    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // Find and set up the ListView for newly discovered devices
    newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:com.example.android.bluetoothchat.LobbyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup the window
    setContentView(R.layout.activity_device_list);

    // Initialize the button to perform device discovery
    ImageButton scanButton = (ImageButton) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doDiscovery();//from  w  w w .  j  a v a  2s .  c om
            v.setVisibility(View.GONE);
        }
    });

    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // Find and set up the ListView for newly discovered devices
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBtAdapter == null)
        return;

    ensureDiscoverable();

}

From source file:com.hwx.wheel.steeringwheel.bluetooth.ScaleActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)/* ww w.  j  a v a2  s  .  co m*/
        Log.e(TAG, "+++ ON CREATE +++");
    setContentView(R.layout.bluetooth_other);
    connect_state = (TextView) findViewById(R.id.connect_state);
    text_data = (TextView) findViewById(R.id.text_data);
    CONNECT_MAC = AppConfig.getInstance().getString("isEmpetConnectMac", "");
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    itemClickAdapter = new ItemClickAdapter(deviceList);
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    LocalBroadcastManager.getInstance(Application.getContext()).registerReceiver(mReceiver, filter);
    new CommandReceiver() {
        @Override
        public void onDataReceived(byte[] buffer, byte function, byte safeCod) {
        }

        @Override
        public void onFail() {
            connect_state.setText("fail connect");
            builder.setLength(0);
            connect_state.postDelayed(() -> startOpen(), 3000);
            startOpen();
        }

        @Override
        public void onLost() {
            connect_state.setText("lost connect");
            builder.setLength(0);
            connect_state.postDelayed(() -> startOpen(), 3000);
        }

        @Override
        public void onDeviceInfo(String name, String address) {
            connect_state.append("\t" + name);
        }

        @Override
        public void onStadeTag(int stade) {
            switch (stade) {
            case 0:
                connect_state.setText("we're doing nothing");
                break;
            case 1:
                connect_state.setText("now listening for incoming connections");
                break;
            case 2:
                connect_state.setText("now initiating an outgoing connection");
                break;
            case 3:
                connect_state.setText("now connected to a remote " + CONNECT_NAME);
                break;
            }
        }
    }.regiest();
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, ControlFragment.newInstance(1))
            .commit();
}

From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    mCordova = cordova;//www  .  jav  a 2s.c  o  m
    try {
        mBtadapter = BluetoothAdapter.getDefaultAdapter();
    } catch (RuntimeException e) {
        XLog.e(CLASS_NAME, "init: RuntimeException");
        return;
    }
    mFoundDevices = new ArrayList<BluetoothDevice>();
    Context context = cordova.getActivity();

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    context.registerReceiver(mReceiver, filter);

    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);
}

From source file:org.jfedor.nxtremotecontrol.ChooseDeviceActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);
    setResult(Activity.RESULT_CANCELED);
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            doDiscovery();// ww  w  .j a va  2  s . c  o m
            v.setVisibility(View.GONE);

        }
    });

    mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedListView.setAdapter(mPairedDevicesArrayAdapter);
    pairedListView.setOnItemClickListener(mDeviceClickListener);

    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

    boolean empty = true;

    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if ((device.getBluetoothClass() != null)
                    && (device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.TOY_ROBOT)) {
                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                empty = false;
            }
        }
    }
    if (!empty) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        findViewById(R.id.no_devices).setVisibility(View.GONE);
    }

    if (checkPlayService()) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

From source file:mobi.monaca.framework.plugin.BluetoothPlugin.java

/**
 * Register receiver as soon as we have the context
 *//*from  www.java 2  s.  co  m*/
@Override
public void setContext(CordovaInterface ctx) {
    super.setContext(ctx);

    //TODO fixed
    // Register for necessary bluetooth events
    ctx.getActivity().registerReceiver(m_bpBroadcastReceiver,
            new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
    ctx.getActivity().registerReceiver(m_bpBroadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    ctx.getActivity().registerReceiver(m_bpBroadcastReceiver, new IntentFilter(BluetoothPlugin.ACTION_UUID));
    //ctx.registerReceiver(m_bpBroadcastReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}

From source file:edu.berkeley.cellscope.cscore.DeviceListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup the window
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);

    // Set result CANCELED in case the user backs out
    setResult(Activity.RESULT_CANCELED);

    // Initialize the button to perform device discovery
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            doDiscovery();/*w  w  w . jav  a 2 s .co m*/
            v.setVisibility(View.GONE);
        }
    });

    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices
    mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // Find and set up the ListView for paired devices
    ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedListView.setAdapter(mPairedDevicesArrayAdapter);
    pairedListView.setOnItemClickListener(mDeviceClickListener);

    // Find and set up the ListView for newly discovered devices
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // Get a set of currently paired devices
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

    // If there are paired devices, add each one to the ArrayAdapter
    if (pairedDevices.size() > 0) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice device : pairedDevices) {
            mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    } else {
        String noDevices = getResources().getText(R.string.none_paired).toString();
        mPairedDevicesArrayAdapter.add(noDevices);
    }
}

From source file:org.lsc.hellocordova.BluetoothPlugin.java

@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
    Log.d("BluetoothPlugin", "Plugin Called");
    PluginResult result = null;//w  w w  .  j a  v a 2s. com
    context = (Context) this.ctx;

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery starts
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when connectivity state changes
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);

    Looper.prepare();
    btadapter = BluetoothAdapter.getDefaultAdapter();
    found_devices = new ArrayList<BluetoothDevice>();

    if (ACTION_DISCOVER_DEVICES.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES);

            found_devices.clear();
            discovering = true;

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            Log.i("BluetoothPlugin", "Discovering devices...");
            btadapter.startDiscovery();

            while (discovering) {
            }

            String devicesFound = null;
            int count = 0;
            devicesFound = "[";
            for (BluetoothDevice device : found_devices) {
                Log.i("BluetoothPlugin",
                        device.getName() + " " + device.getAddress() + " " + device.getBondState());
                if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                    devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ,"
                            + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                            + device.getBluetoothClass().getDeviceClass() + "\" }";
                    if (count < found_devices.size() - 1)
                        devicesFound = devicesFound + ",";
                } else
                    Log.i("BluetoothPlugin",
                            device.getName() + " Problems retrieving attributes. Device not added ");
                count++;
            }

            devicesFound = devicesFound + "] ";

            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound);
            result = new PluginResult(Status.OK, devicesFound);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED);

            boolean isEnabled = btadapter.isEnabled();

            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED,
                    "Returning " + "is Bluetooth Enabled? " + isEnabled);
            result = new PluginResult(Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_ENABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT);

            boolean enabled = false;

            Log.d("BluetoothPlugin", "Enabling Bluetooth...");

            if (btadapter.isEnabled()) {
                enabled = true;
            } else {
                enabled = btadapter.enable();
            }

            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled);
            result = new PluginResult(Status.OK, enabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_DISABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT);

            boolean disabled = false;

            Log.d("BluetoothPlugin", "Disabling Bluetooth...");

            if (btadapter.isEnabled()) {
                disabled = btadapter.disable();
            } else {
                disabled = true;
            }

            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled);
            result = new PluginResult(Status.OK, disabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean paired = false;

            Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName()
                    + " and address " + device.getAddress());

            try {
                Method m = device.getClass().getMethod("createBond");
                paired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired);
            result = new PluginResult(Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_UNPAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean unpaired = false;

            Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("removeBond");
                unpaired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired);
            result = new PluginResult(Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES);

            Log.d("BluetoothPlugin", "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i("BluetoothPlugin",
                            device.getName() + " " + device.getAddress() + " " + device.getBondState());

                    if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                        resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ,"
                                + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                                + device.getBluetoothClass().getDeviceClass() + "\" }";
                        if (count < pairedDevices.size() - 1)
                            resultBoundDevices = resultBoundDevices + ",";
                    } else
                        Log.i("BluetoothPlugin",
                                device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices);
            result = new PluginResult(Status.OK, resultBoundDevices);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT);

            boolean stopped = true;

            Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices...");

            if (btadapter.isDiscovering()) {
                Log.i("BluetoothPlugin", "Stop discovery...");
                stopped = btadapter.cancelDiscovery();
                discovering = false;
            }

            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped);
            result = new PluginResult(Status.OK, stopped);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BOUND_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT);
            String addressDevice = arg1.getString(0);
            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState());

            boolean state = false;

            if (device != null && device.getBondState() == 12)
                state = true;
            else
                state = false;

            Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress());

            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state);
            result = new PluginResult(Status.OK, state);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_OPEN_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_OPEN_SOCKET);
        try {
            String addressDevice = arg1.getString(0);
            openSocket(addressDevice);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_OPEN_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_MESSAGE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_MESSAGE_SOCKET);
        try {
            String msg = arg1.getString(0);
            sendData(msg);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_MESSAGE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_CLOSE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_CLOSE_SOCKET);
        try {
            closeSocket();
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_CLOSE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PING_SOCKET.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_PING_SOCKET);

            String data = "{";
            data = data + "\"count\": " + Integer.toString(pingCount) + ",";

            String items = "";
            for (String item : pingData) {
                items = items + ",\"" + item + "\"";
            }

            data = data + "  \"data\" : [" + items.substring(1) + " ]";
            data = data + "}";

            result = new PluginResult(Status.OK, data);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PING_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    }

    else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("BluetoothPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}

From source file:com.example.android.bluetoothchat.DeviceListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ??/*from   w  w w .  ja va2  s.c o m*/
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_device_list);

    // ????????????CANCELED
    setResult(Activity.RESULT_CANCELED);

    // ????????
    Button scanButton = (Button) findViewById(R.id.button_scan);
    scanButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doDiscovery();
            v.setVisibility(View.GONE);
        }
    });

    //???????
    Button startchatButton = (Button) findViewById(R.id.button_start_chat);
    startchatButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            connect();
        }
    });

    // array adapters??.
    // ??????????????
    ArrayAdapter<String> pairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // ????ListView?
    pairedListView = (ListView) findViewById(R.id.paired_devices);
    pairedListView.setAdapter(pairedDevicesArrayAdapter);
    pairedListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    // ????????ListView?
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    // ????
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // ???
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // local Bluetooth adapter??
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // ????
    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

    // ????????ArrayAdapter??
    if (pairedDevices.size() > 0) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice device : pairedDevices) {
            pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    } else {
        String noDevices = getResources().getText(R.string.none_paired).toString();
        pairedDevicesArrayAdapter.add(noDevices);
    }
}