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

The secret of Tencent Video’s advertising materials to achieve high volume

The author recently met a friend in the same indu...

Why can’t Douyin influencer promotion be charged based on results?

Whether it is performance advertising or brand ad...

The effectiveness and traffic conversion of TikTok advertising in APP!

Recently, advertisers of e-commerce apps seem to ...

The impact of domain names and space on search engines

1. The impact of domain names on search engines (...

How to operate self-media content? 3 tips!

All products must contain content, from as small ...

How much does it cost to be an agent of Shaoguan Coupon Mini Program?

How much does it cost to be an agent for a coupon...

How to quickly get 1000 followers on Douyin?

It is not difficult to achieve the threshold of o...

How do Internet finance platforms retain users?

Although new media operations cannot effectively ...

WeChat and Alipay announce major changes

[[428363]] Good news! WeChat and Alipay announced...