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

Seven rules for Pinduoduo operations

Pinduoduo has improved its operational capabiliti...

Marketing activity rules based on user psychological mechanism

Marketing activities can continuously provide vit...

From 0 to 1 million users, three tricks to retain users

User retention is a difficult problem that every ...

How to do ASO for news and information apps—taking Toutiao as an example!

Mobile Internet is constantly changing our way of...

How the AI ​​behind TikTok works

TikTok is a video-sharing app that allows users t...

A 10,000-word article details the 7 types and 8 steps for creating 100,000+ titles!

Come, before we get started, let’s do a test to s...

iOS StoreKit 2 New Features Analysis

1. Background At WWDC 2021, a new StoreKit 2 libr...

Win2K Server FAQ?

1. Question: My machine has multiple IP addresses...

The ups and downs of Chinese advertising companies over the past 40 years

*Commercial advertising was once accused of being...