杂七杂八APK-蓝牙

        客户要求一个能判断蓝牙是否正常工作的apk,故写了一个特别简单的收发蓝牙数据的demo,蓝牙需要先打开并绑定,再次点击是停止发送数据,客户要求比较简单,搜索蓝牙和切换绑定设备发送数据都没做。

启动的时候添加所有已绑定过的蓝牙设备,并将其加入到数据列表

 Set<BluetoothDevice> devices;

  devices = mBluetoothAdapter.getBondedDevices();  
        if (devices.size() > 0) {  
            for (BluetoothDevice bluetoothDevice : devices) {  
                // 保存到arrayList集合中  
                bluetoothDevices.add(bluetoothDevice.getName() + ":"  
                        + bluetoothDevice.getAddress() + "\n");  
                
            }  
        } 

然后开启一个循环接收数据的线程

// 服务端接收信息线程  
    private class AcceptThread extends Thread {  
        private BluetoothServerSocket serverSocket;// 服务端接口  
        private BluetoothSocket socket;// 获取到客户端的接口  
        private InputStream is;// 获取到输入流  
        private OutputStream os;// 获取到输出流  
 
        public AcceptThread() {  
            try {  
                // 通过UUID监听请求,然后获取到对应的服务端接口  
                serverSocket = mBluetoothAdapter  
                        .listenUsingRfcommWithServiceRecord(NAME, MY_UUID);  //客户端和服务端的UUID要一致
            } catch (Exception e) {  
                // TODO: handle exception  
            }  
        }  
 
        public void run() {  
            try {  
                // 接收其客户端的接口  
                socket = serverSocket.accept();  
                // 获取到输入流  
                is = socket.getInputStream();  
                // 获取到输出流  
                os = socket.getOutputStream();  
 
                // 无线循环来接收数据  
                while (true) {  
                    // 创建一个128字节的缓冲  
                    byte[] buffer = new byte[128];  
                    // 每次读取128字节,并保存其读取的角标  
                    int count = is.read(buffer);  
                    // 创建Message类,向handler发送数据  
                    Message msg = new Message();  
                    // 发送一个String的数据,让他向上转型为obj类型  
                    msg.obj = new String(buffer, 0, count, "utf-8");  
                    // 发送数据  
                    handler.sendMessage(msg);  
                }  
            } catch (Exception e) {  
                // TODO: handle exception  
                e.printStackTrace();  
            }  
 
        }  
    } 

在onItemClick点击之后开启一个不断发送数据的线程,代码比较,简单贴一下
            // 判断客户端接口是否为空  

            if (clientSocket == null) {  
                // 获取到客户端接口  
                clientSocket = selectDevice  
                        .createRfcommSocketToServiceRecord(MY_UUID);  //不能重复绑定
                // 向服务端发送连接  
                clientSocket.connect();  
                // 获取到输出流,向外写数据  
                os = clientSocket.getOutputStream();  
                os.write(text.getBytes("UTF-8"));
            } 

 

PS,蓝牙连接需要UUID来建立连接,我这没有找到如何解除这个绑定,又不能重复绑定,所以想给其他设备发送数据需要后台关掉该应用。以下是完整代码,搜索功能被我注掉了,有需要的可以自己打开,如其他地方找到了解绑的api,可以留言。

public class MainActivity extends Activity implements OnItemClickListener {  
    // 获取到蓝牙适配器  
    private BluetoothAdapter mBluetoothAdapter;  
    // 用来保存搜索到的设备信息  
    private List<String> bluetoothDevices = new ArrayList<String>();  
    // ListView组件  
    private ListView lvDevices;  
    // ListView的字符串数组适配器  
    private ArrayAdapter<String> arrayAdapter;  
    // UUID,蓝牙建立链接需要的  
    private final UUID MY_UUID = UUID  
            .fromString("db764ac8-4b08-7f25-aafe-59d03c27bae3");  
    // 为其链接创建一个名称  
    private final String NAME = "Bluetooth_Socket";  
    // 选中发送数据的蓝牙设备,全局变量,否则连接在方法执行完就结束了  
    private BluetoothDevice selectDevice;  
    // 获取到选中设备的客户端串口,全局变量,否则连接在方法执行完就结束了  
    private BluetoothSocket clientSocket;  
    // 获取到向设备写的输出流,全局变量,否则连接在方法执行完就结束了  
    private OutputStream os;  
    // 服务端利用线程不断接受客户端信息  
    private AcceptThread thread;  
    int count = 1;//发送的数据次数
    boolean isPause = true;
    Set<BluetoothDevice> devices;
    private  String bondedDevice;
 
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        // 获取到蓝牙默认的适配器  
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
        // 获取到ListView组件  
        lvDevices = (ListView) findViewById(R.id.lvdevices);  
       if (!mBluetoothAdapter.isEnabled()) {
           mBluetoothAdapter.enable();
       }
        // 为listview设置字符换数组适配器  
        arrayAdapter = new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_1, android.R.id.text1,  
                bluetoothDevices);  
        // 为listView绑定适配器  
        lvDevices.setAdapter(arrayAdapter);  
        // 为listView设置item点击事件侦听  
        lvDevices.setOnItemClickListener(this);  
 
        // 用Set集合保持已绑定的设备  
       devices = mBluetoothAdapter.getBondedDevices();  
        if (devices.size() > 0) {  
            for (BluetoothDevice bluetoothDevice : devices) {  
                // 保存到arrayList集合中  
                bluetoothDevices.add(bluetoothDevice.getName() + ":"  
                        + bluetoothDevice.getAddress() + "\n");  
                
            }  
        }  
        // 因为蓝牙搜索到设备和完成搜索都是通过广播来告诉其他应用的  
        // 这里注册找到设备和完成搜索广播  
        IntentFilter filter = new IntentFilter(  
                BluetoothAdapter.ACTION_DISCOVERY_FINISHED);  
        registerReceiver(receiver, filter);  
        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
        registerReceiver(receiver, filter);  
 
        // 实例接收客户端传过来的数据线程  
        thread = new AcceptThread();  
        // 线程开始  
        thread.start();  
        
        
     
    }  
 
    public void onClick_Search(View view) {  
        /*setTitle("正在扫描...");  
        // 点击搜索周边设备,如果正在搜索,则暂停搜索  
        if (mBluetoothAdapter.isDiscovering()) {  
            mBluetoothAdapter.cancelDiscovery();  
        }  
        mBluetoothAdapter.startDiscovery();  */
    }  
 
    // 注册广播接收者  
    private BroadcastReceiver receiver = new BroadcastReceiver() {  
        @Override  
        public void onReceive(Context arg0, Intent intent) {  
            // 获取到广播的action  
            String action = intent.getAction();  
            // 判断广播是搜索到设备还是搜索完成  
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {  
                // 找到设备后获取其设备  
                BluetoothDevice device = intent  
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
                // 判断这个设备是否是之前已经绑定过了,如果是则不需要添加,在程序初始化的时候已经添加了  
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {  
                    // 设备没有绑定过,则将其保持到arrayList集合中  
                    if (bluetoothDevices.contains(device.getName() + ":"  
                                + device.getAddress() + "\n")) {
                        bluetoothDevices.add(device.getName() + ":"  
                                + device.getAddress() + "\n");  
                        // 更新字符串数组适配器,将内容显示在listView中  
                        arrayAdapter.notifyDataSetChanged();  
                    }

                }  
            } else if (action  
                    .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {  
                setTitle("搜索完成");  
            }  
        }  
    };  
 
    SimpleDateFormat   formatter ;
    Date curDate ;
    String str;
    
    // 点击listView中的设备,传送数据  
    @Override  
    public void onItemClick(AdapterView<?> parent, View view, int position,  
            long id) {  
        // 获取到这个设备的信息  
        isPause = !isPause;//是否发送数据
        String s = arrayAdapter.getItem(position);  
        // 对其进行分割,获取到这个设备的地址  
        String address = s.substring(s.indexOf(":") + 1).trim();  
        // 判断当前是否还是正在搜索周边设备,如果是则暂停搜索  
        if (mBluetoothAdapter.isDiscovering()) {  
            mBluetoothAdapter.cancelDiscovery();  
        }  
        // 如果选择设备为空则代表还没有选择设备  
        if (selectDevice == null ) {  
            //通过地址获取到该设备  
            
            selectDevice = mBluetoothAdapter.getRemoteDevice(address);  
            
            String string = bluetoothDevices.get(position);
           bluetoothDevices.set(position, string+ "-- 已绑定");
           bondedDevice = string+ "-- 已绑定";
           arrayAdapter.notifyDataSetChanged();
        }  
        // 这里需要try catch一下,以防异常抛出  
           formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");     
         
        try {  
            // 判断客户端接口是否为空  

            if (clientSocket == null) {  
                // 获取到客户端接口  
                clientSocket = selectDevice  
                        .createRfcommSocketToServiceRecord(MY_UUID);  
                // 向服务端发送连接  
                clientSocket.connect();  
                // 获取到输出流,向外写数据  
                os = clientSocket.getOutputStream();  
 
            }  
            // 判断是否拿到输出流
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while (!isPause) {
                        if (os != null) {  
                            // 需要发送的信息  
                            curDate =  new Date(System.currentTimeMillis());
                            str   =   formatter.format(curDate);  
                            String text = Integer.toString(count) + "  " + str;  
                            // 以utf-8的格式发送出去  
                            try {
                                os.write(text.getBytes("UTF-8"));
                                Thread.sleep(1000);
                            } catch (UnsupportedEncodingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }  
                            count++;
                        }  
                        
                    }
                    
                }
            }).start();
            
            // 吐司一下,告诉用户发送成功  
            Toast.makeText(this, "发送信息成功,请查收", 0).show();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            // 如果发生异常则告诉用户发送失败  
            Toast.makeText(this, "发送信息失败", 0).show();  
        }  
    }  
 
    // 创建handler,因为我们接收是采用线程来接收的,在线程中无法操作UI,所以需要handler  
    Handler handler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {  
            // TODO Auto-generated method stub  
            super.handleMessage(msg);  
            // 通过msg传递过来的信息,吐司一下收到的信息  
            if (bluetoothDevices.size() >= 10) {//界面数据太多就清除数据,并重新添加绑定的蓝牙数据
                bluetoothDevices.clear();
                bluetoothDevices.add(bondedDevice);  
                arrayAdapter.notifyDataSetChanged();
            }
            bluetoothDevices.add(msg.obj.toString());
            arrayAdapter.notifyDataSetChanged();
            //Toast.makeText(MainActivity.this, (String) msg.obj, 0).show();  
        }  
    };  
 
    // 服务端接收信息线程  
    private class AcceptThread extends Thread {  
        private BluetoothServerSocket serverSocket;// 服务端接口  
        private BluetoothSocket socket;// 获取到客户端的接口  
        private InputStream is;// 获取到输入流  
        private OutputStream os;// 获取到输出流  
 
        public AcceptThread() {  
            try {  
                // 通过UUID监听请求,然后获取到对应的服务端接口  
                serverSocket = mBluetoothAdapter  
                        .listenUsingRfcommWithServiceRecord(NAME, MY_UUID);  
            } catch (Exception e) {  
                // TODO: handle exception  
            }  
        }  
 
        public void run() {  
            try {  
                // 接收其客户端的接口  
                socket = serverSocket.accept();  
                // 获取到输入流  
                is = socket.getInputStream();  
                // 获取到输出流  
                os = socket.getOutputStream();  
 
                // 无线循环来接收数据  
                while (true) {  
                    // 创建一个128字节的缓冲  
                    byte[] buffer = new byte[128];  
                    // 每次读取128字节,并保存其读取的角标  
                    int count = is.read(buffer);  
                    // 创建Message类,向handler发送数据  
                    Message msg = new Message();  
                    // 发送一个String的数据,让他向上转型为obj类型  
                    msg.obj = new String(buffer, 0, count, "utf-8");  
                    // 发送数据  
                    handler.sendMessage(msg);  
                }  
            } catch (Exception e) {  
                // TODO: handle exception  
                e.printStackTrace();  
            }  
 
        }  
    }  

 

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值