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

Is Shield TV really that amazing? Is NVIDIA obsessed with streaming?

Android game consoles have been around for a whil...

How to get traffic from Tik Tok short videos

Tik Tok short video marketing is booming, how can...

Tesla's sales are slowing down? No, this is just the beginning!

How long has it been since Tesla launched a new m...

How to achieve new user traffic across the entire network?

Recently, the E-Commerce Research Center of the C...

Apple iCloud briefly down, possibly to fix new iOS 8 vulnerability

Apple has confirmed that iCloud has temporarily b...

Honda joins Baidu's Apollo platform to make self-driving cars a reality

Recently, according to a report by the Nikkei, Ho...

A comprehensive summary of mobile UI design specifications

When many newcomers start to design mobile UI, th...

How do offline stores build a user growth system?

How do offline stores build a user growth system?...

Apple sued for baseband infringement: Intel and Qualcomm both hit

ParkerVision, a Florida-based RF (radio frequency)...