2019年10月30日 星期三

Android ble app: record .wav file which audio stream via ble (Mono, 16000Hz)

This is an example for android app supposed to:


use ble device: nordic thingy:52 
Audio Source: thingy:52 mic
(Mono, 16 bits ADC, sample rate:16000 Hz)



app flow chart:


In AndroidManifest.xml, require permission:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android.bluetoothlegatt"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
    
    <!-- Declare this required feature if you want to make the app available to BLE-capable
    devices only.  If you want to make your app available to devices that don't support BLE,
    you should omit this in the manifest.  Instead, determine BLE capability by using
    PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE) -->

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>


    <application android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher3"
        android:roundIcon="@mipmap/ic_launcher3_round"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Holo.Light">
        <activity android:name=".DeviceScanActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".DeviceControlActivity"/>
        <service android:name=".BluetoothLeService" android:enabled="true"/>
    </application>

</manifest>

But also need to ask again in activity:
In DeviceControlActivity.java:

 mStartRecord.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                mRecordInfo.setText("Press start record");
                if(checkStoragePermission2())
                {
                    mRecordInfo.setText("start record...");
                    Intent intent = new Intent(getApplicationContext(), BluetoothLeService.class);
                    intent.setAction(BluetoothLeService.ACTION_START_RECORDING_SERVICE);
                    startService(intent);
                }
            }
        });
public boolean checkStoragePermission2(){
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                        new String[]{
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE},
                        REQ_CODE_READ_EXTERNAL_STORAGE_IMPORT);
                return false;
            }
        }
        return true;
    }

in BluetoothLeService.java, a handler to request mtu(ble Max Transmit Unit) to 276 bytes(default 23 bytes):

  public final static UUID UUID_HEART_RATE_MEASUREMENT =
            UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
    public final static UUID THINGY_MICROPHONE_CHARACTERISTIC =
            UUID.fromString(SampleGattAttributes.THINGY_MICROPHONE_CHARACTERISTIC);

    private int mMtu;
    private int mtu = 276;
    private final Handler mMtuHandler;


after connected:

  @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.i(TAG, "Attempting to start service discovery:" +
                        mBluetoothGatt.discoverServices());
                mMtuHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "requestMtu:"+mMtu);
                        if (mBluetoothGatt != null) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                            {
                                while(mMtu!=276)
                                {
                                    Log.i(TAG, "mMtu length"+mMtu);
                                    if (mMtu < mtu) {
                                        boolean isMtuRequestSuccess = mBluetoothGatt.requestMtu(276);
                                        Log.i(TAG, "mMtu length in request="+isMtuRequestSuccess);
                                    }
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e){
                                        e.printStackTrace();
                                    }
                                }
                            } else {
                                mMtu = 23;
                                Log.i(TAG, "mMtu length in else"+mMtu);
                            }
                        }
                    }
                }, 1000);

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);

            }
        }

find mtu changed(onMtuChanged):
  @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
            if (status == BluetoothGatt.GATT_SUCCESS){
                Log.i(TAG, "onMtuChanged() " + mtu + " Status: " + status);
                mMtu = mtu;
            }
            else
            {
                Log.i(TAG, "MTU configuration failed with error:"+ status);
            }
        }


after find sound service, notify Microphone characteristic,new ADCDecoder, file Stream:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        // This is specific to Heart Rate Measurement.
        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
        else if(THINGY_MICROPHONE_CHARACTERISTIC.equals(characteristic.getUuid()))
        {
            final BluetoothGattDescriptor microphoneDescriptor = characteristic.getDescriptor( UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            microphoneDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(microphoneDescriptor);

            enableAdpcmMode(true);
        }
    }



enableAdpcmMode:

private void enableAdpcmMode(final boolean enable)
    {        
        mAdpcmDecoder = new ADPCMDecoder(mContext,false);       
    }


in DeviceControlActivity.java press 3 buttons to send intent to BluetoothLeService:

(the above members in BluetoothLeServie.java )

    private ADPCMDecoder mAdpcmDecoder;
    private final Handler mRecordHandler;
    //记录播放状态
    private boolean isRecording = false;
    private boolean isRecordUnsave = false;
    //数字信号数组
    private byte [] noteArray;
    //PCM文件
    private File pcmFile;
    //WAV文件
    private File wavFile;
    //文件输出流
    private OutputStream os;
    //文件根目录
    private String basePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/thingy_voice";
    //wav文件目录
    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    private String outFileName = basePath+"/agatha_"+format.format(date)+".wav";
    //pcm文件目录
    private String inFileName = basePath+"/yinfu.pcm";

Buttons in Activity:
 mStartRecord = (Button) findViewById(R.id.startRecord_bt);
        mStopRecord = (Button)findViewById(R.id.stopRecord_bt);
        mTransWav_bt = (Button)findViewById(R.id.transWav_bt);
        mRecordInfo = (TextView)findViewById(R.id.recordInfo);
        mMicStreamInfo = (TextView)findViewById(R.id.streamInfo);


        mStartRecord.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                mRecordInfo.setText("Press start record");
                if(checkStoragePermission2())
                {
                    mRecordInfo.setText("start record...");
                    Intent intent = new Intent(getApplicationContext(), BluetoothLeService.class);
                    intent.setAction(BluetoothLeService.ACTION_START_RECORDING_SERVICE);
                    startService(intent);
                }
            }
        });
        mStopRecord.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                mRecordInfo.setText("Press stop record");
                Intent intent = new Intent(getApplicationContext(), BluetoothLeService.class);
                intent.setAction(BluetoothLeService.ACTION_STOP_RECORDING_SERVICE);
                startService(intent);
            }
        });
        mTransWav_bt.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                mRecordInfo.setText("Press save as WAV file");
                Intent intent = new Intent(getApplicationContext(), BluetoothLeService.class);
                intent.setAction(BluetoothLeService.ACTION_START_TRANS_WAV);
                startService(intent);
            }

        });

Action in BluetoothLeService.java:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            String action = intent.getAction();
            if (action != null && !action.isEmpty())
                switch (action){
                    case ACTION_START_RECORDING_SERVICE:
                        Log.i(TAG,"ACTION_START_RECORDING_SERVICE");
                        createFile();
                        startRecord();
                        break;
                    case ACTION_STOP_RECORDING_SERVICE:
                        Log.i(TAG,"ACTION_STOP_RECORDING_SERVICE");
                        stopRecord();
                        break;
                    case ACTION_START_TRANS_WAV:
                        Log.i(TAG,"ACTION_START_TRANS_WAV");
                        convertWaveFile();
                        break;

                }
            }
        return super.onStartCommand(intent, flags, startId);
    }



 public void createFile(){
        File baseFile = new File(basePath);

        if(!baseFile.exists())
            baseFile.mkdirs();
        pcmFile = new File(basePath+"/yinfu.pcm");
        wavFile = new File(basePath+"/agatha.wav");
        if(pcmFile.exists()){
            pcmFile.delete();
        }
        if(wavFile.exists()){
            wavFile.delete();
        }
        try{
            boolean i = pcmFile.createNewFile();
            Log.i(TAG,"create pcmfile:"+i);
            boolean j =wavFile.createNewFile();
            Log.i(TAG,"create pcmfile:"+j+",base"+basePath);
            os = new BufferedOutputStream(new FileOutputStream(pcmFile));
        }catch(IOException e){
            Log.i(TAG,e.toString());
        }
    }

    public void startRecord(){
        isRecording = true;
    }

    public void stopRecord(){
        isRecording = false;
    }

    public void convertWaveFile(){
        FileInputStream in = null;
        FileOutputStream out = null;
        long totalAudioLen = 0;
        long totalDataLen = totalAudioLen + 36;
        long longSampleRate = 16000;
        int channels = 1;
        long byteRate = 16 *longSampleRate* channels / 8;
        byte[] data = new byte[512];
        try{
            in = new FileInputStream(inFileName);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String outFileName_new = basePath+"/agatha_"+format.format(date)+".wav";
            out = new FileOutputStream(outFileName_new);
            totalAudioLen = in.getChannel().size();
            totalDataLen = totalAudioLen + 36;
            WriteWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);
            while (in.read(data) != -1) {
                out.write(data);
            }
            in.close();
            out.close();

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String outFileName_new = basePath+"/agatha_"+format.format(date)+".wav";
        final Intent intent = new Intent(ACTION_FINISH_WAV);
        intent.putExtra(EXTRA_DATA,outFileName_new);
        sendBroadcast(intent);

    }
    //https://blog.csdn.net/chezi008/article/details/53064604
    //https://blog.csdn.net/tong5956/article/details/82687001
    //https://blog.xuite.net/john75310/wretch/137622947-%5BAndroid%5D+AudioRecord+%E9%8C%84%E8%A3%BD+Wav+File
    private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen, long totalDataLen, long longSampleRate,
                                     int channels, long byteRate) throws IOException {
        byte[] header = new byte[44];
        header[0] = 'R'; // RIFF
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (totalDataLen & 0xff);//数据大小
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = 'W';//WAVE
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';
        //FMT Chunk
        header[12] = 'f'; // 'fmt '
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';//过渡字节
        //数据大小
        header[16] = 16; // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        //编码方式 10H为PCM编码格式
        header[20] = 1; // format = 1
        header[21] = 0;
        //通道数
        header[22] = (byte) channels;
        header[23] = 0;
        //采样率,每个通道的播放速度
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        //音频数据传送速率,采样率*通道数*采样深度/8
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        // 确定系统一次要处理多少个这样字节的数据,确定缓冲区,通道数*采样位数
        header[32] = (byte) (1 * 16 / 8);
        header[33] = 0;
        //每个样本的数据位数
        header[34] = 16;
        header[35] = 0;
        //Data chunk
        header[36] = 'd';//data
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
        out.write(header, 0, 44);
    }
how to use:

the file will save in /storage0 

     //文件根目录
    private String basePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/thingy_voice";
    //wav文件目录
    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    private String outFileName = basePath+"/agatha_"+format.format(date)+".wav";
    //pcm文件目录
    private String inFileName = basePath+"/yinfu.pcm";



Refereance: