Android source code download: Bluetooth_4.3 BLE Bluetooth communication

Android source code download: Bluetooth_4.3 BLE Bluetooth communication

Functional classification: Tools

Supported platforms: Android

Operating environment: Eclipse

Development language: Java

Development tool: Eclipse

Source code size: 1.37MB

Source code download address: http://down..com/data/1984795

Source code introduction

This demo is mainly compatible with Bluetooth communication between Android 4.3 terminal devices that support BLE.

Key features include:

1. Turn Bluetooth on and off;

2. Search and connect to surrounding Bluetooth enabled devices

3. After successfully establishing the connection, send a message to the other party via Bluetooth.

Note: To observe the sent/received messages, both parties must have this APP installed.

Source code running screenshot

When the APP turns on Bluetooth, the request for Bluetooth

Bluetooth communication

[[128207]]

Search for nearby Bluetooth devices with the app installed

Set some Bluetooth property values

Source code snippet

  1. private   void scanLeDevice( final   boolean enable) {

    if (enable) {

  2. // Stops scanning after a pre-defined scan period.  

    mHandler.postDelayed( new Runnable() {

  3. @Override  

    public   void run() {

  4. mScanning = false ;

    mBluetoothAdapter.stopLeScan(mLeScanCallback);

  5. invalidateOptionsMenu();

    }

  6. }, SCAN_PERIOD);

      

  7. mScanning = true ;

    mBluetoothAdapter.startLeScan(mLeScanCallback);

  8. } else {

    mScanning = false ;

  9. mBluetoothAdapter.stopLeScan(mLeScanCallback);

    }

  10. invalidateOptionsMenu();

    }

  11.   

    // Adapter for holding devices found through scanning.  

  12. private   class LeDeviceListAdapter extends BaseAdapter {

    private ArrayList<bluetoothdevice> mLeDevices;

  13. private LayoutInflater mInflator;

      

  14. public LeDeviceListAdapter() {

    super ();

  15. mLeDevices = new ArrayList<bluetoothdevice>();

    mInflator = DeviceScanActivity. this .getLayoutInflater();

  16. }

      

  17. public   void addDevice(BluetoothDevice device) {

    if (!mLeDevices.contains(device)) {

  18. mLeDevices.add(device);

    }

  19. }

      

  20. public BluetoothDevice getDevice( int position) {

    return mLeDevices.get(position);

  21. }

      

  22. public   void clear() {

    mLeDevices.clear();

  23. }

      

  24. @Override  

    public   int getCount() {

  25. return mLeDevices.size();

    }

  26.   

    @Override  

  27. public Object getItem( int i) {

    return mLeDevices.get(i);

  28. }

      

  29. @Override  

    public   long getItemId( int i) {

  30. return i;

    }

  31.   

    @Override  

  32. public View getView( int i, View view, ViewGroup viewGroup) {

    ViewHolder viewHolder;

  33. // General ListView optimization code.  

    if (view == null ) {

  34. view = mInflator.inflate(R.layout.listitem_device, null );

    viewHolder = new ViewHolder();

  35. viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);

    viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);

  36. view.setTag(viewHolder);

    } else {

  37. viewHolder = (ViewHolder) view.getTag();

    }

  38.   

    BluetoothDevice device = mLeDevices.get(i);

  39. final String deviceName = device.getName();

    if (deviceName != null && deviceName.length() > 0 )

  40. viewHolder.deviceName.setText(deviceName);

    else  

  41. viewHolder.deviceName.setText(R.string.unknown_device);

    viewHolder.deviceAddress.setText(device.getAddress());

  42.   

    return view;

  43. }

    }

  44.   

    // Device scan callback.  

  45. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

      

  46. @Override  

    public   void onLeScan( final BluetoothDevice device, int rssi, byte [] scanRecord) {

  47. runOnUiThread( new Runnable() {

    @Override  

  48. public   void run() {

    mLeDeviceListAdapter.addDevice(device);

  49. mLeDeviceListAdapter.notifyDataSetChanged();

    }

  50. });

    }

  51. };

    </bluetoothdevice></bluetoothdevice>

Source code download address: http://down..com/data/1984795

<<:  While others are grabbing red envelopes, programmers are studying red envelope algorithms

>>:  Android application source code for smart agriculture

Recommend

Let’s take a look at BAT’s layout in the AR field. How would you rate it?

[[170825]] The so-called AR (augmented reality) i...

How to wake up a sleeping user

User wake-up In the field of mobile Internet , CA...

In-depth understanding of OC/C++ closures

Author: Cui Xiaobing background Apple's Objec...

How much does the video of the African black mercenaries shouting cost?

The African black mercenaries shouting was shot b...

How to apply for a 400 telephone number? How to apply for a 400 number?

How to apply for a 400 telephone number? How to a...