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

Will backward compatibility be the savior to save Xbox One sales?

The release of WIN10 can be said to be one of Mic...

Apple releases new iOS 12.3 beta: Improved speed and stability

Apple has released the fourth beta version of iOS...

How many colors does the earth have from the perspective of space?

The brilliant light at the edge of the earth What...

This little bird definitely knows fashion, how beautiful the kingfisher is

China Science and Technology News Network, Decemb...

How to make money through online promotion?

1. We-media It refers to the general term for new...

Counterpoint: Global cellular IoT module shipments fell 2% in Q3 2023

According to the latest data from Counterpoint, g...

A Guide to Private Domain Marketing for Fast Moving Consumer Goods

Today, we have entered the era of new retail and ...

From bloom to withering: Blackberry is a wake-up call for Apple

Apple's recent financial report shows that al...

Principal Jiaqi's "Exclusive for Douyin Fans - Heart Hunting Plan"

Principal Jiaqi's "Douyin Fans Exclusive...

Apple's hardcore fans complain: software quality is declining

Marc Almonte, who enjoys a high reputation in the...